So I have this data frame
df <- data.frame( A=1:10, B=LETTERS[1:10], C=letters[1:10], stringsAsFactors= F )
I want to add a row to this data frame with A=11 and C="K", but I don't have the information on the column C. In fact, I have no idea a priori what other columns are there in the data frame, except for A and B. These missing columns should get an NA. The best I could come up is this:
tmp <- rep( NA, ncol( df ) ) # Batman!
df <- rbind( df, tmp )
df[ nrow( df ), "A" ] <- 11
df[ nrow( df ), "B" ] <- "K"
Is there a simpler way?
See Question&Answers more detail:os