Search code examples
rdataframer-factor

invalid factor level data.frame error inspite of stringsAsFactors=FALSE


I have factor level problem. I thought it would be solved with stringAsFactors=FALSE, but it's not working.

here pL is a list with 290 elements. I would like to define an empty data.frame, than fill it using rbind.

ttable <- data.frame(ID_REF=c(1,2,3,4,5,6), IDENTIFIER=c("ERN2", "HTR5A", "ACPP", "GNAO1", "HTR1F", "DNAH1"), GSM11708=c("<NA>", 1.994, "<NA>","<NA>","<NA>","<NA>"), GSM11735=c(0.18, "<NA>","<NA>","<NA>","<NA>","<NA>"))
pL <- list("GSTT4", "AHRR", "HAX1", "DNM1L", "MEIS1", "SLC17A3", "CES2", "MLL2", "IKBKB", "GSTA4")

gn <- data.frame(gn = character(0), stringsAsFactors=FALSE)

for(i in pL){
  n <- nrow(subset(ttable, IDENTIFIER==i))
  if (n < 1){
    gn <- rbind(gn, i)
  }
  else{
    for(j in 1:n){
      gn <- rbind(gn, i)
    }
  }
}

Solution

  • Hard to help without a reproducible example. But this should work:

    gn1 <- unlist(lapply(pL,function(i){
      n <- nrow(subset(ttable, IDENTIFIER==i))
      gn <- if (n < 1) i  else seq(n)
    }))
    

    z I run your code I get gn data.frame. You run mine you get gn1 vector. I cbind the 2 for comparison.

        cbind(as.data.frame(gn1),gn)
           gn1 X.GSTT4.
    1    GSTT4    GSTT4
    2     AHRR     AHRR
    3     HAX1     HAX1
    4    DNM1L    DNM1L
    5    MEIS1    MEIS1
    6  SLC17A3  SLC17A3
    7     CES2     CES2
    8     MLL2     MLL2
    9    IKBKB    IKBKB
    10   GSTA4    GSTA4
    

    As you seed the 2 columns are identical.