R: Unable to rbind more than one observation to a

2019-08-26 21:21发布

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?

标签: r rbind
0条回答
登录 后发表回答