When you do
c("one","lala",1)
this creates a row vector. 1 converted to a character type, so that all elements in the vector are of the same type.
Then rbind(a,b) will try to combine a , which is the data frame, and b which is the character symbol, and that is not what you want.
A way to do this is with rbind with data frame objects.
a <- NULL b <- data.frame(A="one", B="lala", C=1) d <- data.frame(A="two", B="lele", C=2) a <- rbind(a, b) a <- rbind(a, d)
Now we see that the columns in data frame a are the correct type.
> lapply(a, class) $A [1] "factor" $B [1] "factor" $C [1] "numeric" >
Please note that you must specify columns when creating different data frame, otherwise rbind will fail. If you do
b <- data.frame("one", "lala", 1) d <- data.frame("two", "lele", 2)
then
> rbind(b, d) Error in match.names(clabs, names(xi)) : names do not match previous names
source share