0

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)
    }
  }
}
tonytonov
  • 25,060
  • 16
  • 82
  • 98
charisz
  • 302
  • 4
  • 12
  • 3
    Without data we can't reproduce your error. You should also avoid using `rbind` inside a loop. – agstudy Jul 17 '13 at 10:27
  • You could try `rbind(gb, i, stringsAsFactors=FALSE)` or set the global option. However, @agstudy gave very relevant advice. Growing an object in a loop is slooooowwww. – Roland Jul 17 '13 at 10:48
  • @agstudy Could you write too please, what else should I use? – charisz Jul 17 '13 at 10:52
  • you final gn is just a vector of index? – agstudy Jul 17 '13 at 10:54
  • @Roland What else than loop can i use, when I have a list, and I should search the items of the list in a special data.frame object (from the biological GEO database)? – charisz Jul 17 '13 at 10:56
  • My final gn is a dataframe, with one column (with column name), it will be a column in a bigger table (with cbind). – charisz Jul 17 '13 at 10:58
  • Well, usually you would use `lapply` and relatives. Give a [reproducible example](http://stackoverflow.com/a/5963610/1412059) and there is a good chance that someone will show you how. – Roland Jul 17 '13 at 10:58
  • I add an reproducible example. – charisz Jul 17 '13 at 11:22

1 Answers1

0

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.

agstudy
  • 119,832
  • 17
  • 199
  • 261