When you create the dataframe you have the choice of making your string columns factors (stringsAsFactors=T
), or keeping them as strings.
For your case, don't make your string columns factors. Keep them as strings, then appending works fine. If you need them to ultimately be factors, do all the insertion and appending first as string, then finally convert them to factor.
If you make the string columns factors and then append rows containing unseen values, you get the error you mentioned on each new unseen factor level and that value gets replaced with NA...
> df <- data.frame(patient=c('Ann','Bob','Carol'), referring_doctor=c('X','Y','X'), stringsAsFactors=T)
patient referring_doctor
1 Ann X
2 Bob Y
3 Carol X
> df <- rbind(df, c('Denise','Z'))
Warning messages:
1: In `[<-.factor`(`*tmp*`, ri, value = "Denise") :
invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, ri, value = "Z") :
invalid factor level, NA generated
> df
patient referring_doctor
1 Ann X
2 Bob Y
3 Carol X
4 <NA> <NA>
So don't make your string columns factors. Keep them as strings, then appending works fine:
> df <- data.frame(patient=c('Ann','Bob','Carol'), referring_doctor=c('X','Y','X'), stringsAsFactors=F)
> df <- rbind(df, c('Denise','Z'))
patient referring_doctor
1 Ann X
2 Bob Y
3 Carol X
4 Denise Z
To change the default behavior:
options(stringsAsFactors=F)
To convert individual columns to/from string or factor
df$col <- as.character(df$col)
df$col <- as.factor(df$col)
rbind
is stated: "Factors have their levels expanded as necessary" (R-2.9.2). Maybe you could check exactly which column causes this?read.csv(..., stringsAsFactors=FALSE
andoptions(stringsAsFactors=FALSE)
. There are lots of questions on SO.