R: Unable to rbind more than one observation to a data frame

萝らか妹 提交于 2019-12-11 16:48:52

问题


I've used data.table to create an empty data frame but when I'm adding observations to it all the observations after the first one get coerced to NA.

First I create an empty data frame using the "stringsAsFactors = FALSE" parameter:

cards <- data.frame(Ending = character(), Owner = character(), stringsAsFactors = FALSE)
str(cards)
'data.frame':   0 obs. of  2 variables:
 $ Ending: chr 
 $ Owner : chr 

I get a data frame with 0 observations and two character variables as expected.

Then I create observation 1 (not the actual code - extracted from a another data.frame DF)

newcard1 <- as.list(
   c(Ending = as.character(DF[4, "Col1"]), 
      Owner = as.character(DF[4, "Col2"]))
   )
str(newcard1)
List of 2
 $ Ending: chr "1234"
 $ Owner : chr "JOE BIDEN"

and observation 2 (not the actual code - extracted from a another data.frame DF)

newcard2 <- as.list(
  c(Ending = as.character(DF[51, "Col1"]), 
    Owner = as.character(DF[51, "Col2"]))
   )
str(newcard2)
List of 2
$ Ending: chr "5678"
$ Owner : chr "JOHN TURTURRO"

Both are a list with two character variables as shown above.

Now if I bind the first observation works

cards <- rbind(cards, newcard1)
str(cards)
'data.frame':   1 obs. of  2 variables:
  $ Ending: Factor w/ 1 level "1234": 1
  $ Owner : Factor w/ 1 level "JOE BIDEN: 1

the rbind operation has changed the observation into Factors. In addition, if I try to add any other observations, rbind won't accept any other value as valid level and coerces the values to NA:

cards <- rbind(cards, newcard2)
Warning messages:
1: In `[<-.factor`(`*tmp*`, ri, value = FALSE) :
   invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, ri, value = FALSE) :
   invalid factor level, NA generated
str(cards)
'data.frame':  2 obs. of  2 variables:
  $ Ending: Factor w/ 1 level "1234": 1 NA
  $ Owner : Factor w/ 1 level "JOE BIDEN": 1 NA

cards
   Ending       Owner
 1 1234         JOE BIDEN
 2 <NA>         <NA>

what am I doing wrong?

来源:https://stackoverflow.com/questions/48756144/r-unable-to-rbind-more-than-one-observation-to-a-data-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!