I have a dataframe which contains both numeric variables and factors.
When moving data from one dataframe to another, everything is kept as I would like it:
copy_data<-as.data.frame(original_data)
This creates a copy of ‘original_data’ with factors remaining factors.
When I try a more complex version, the end result is a dataframe of numeric values, when I want the factors to still be factors:
model_data<-with(subset(copy_data, copy_data$var1<0),
as.data.frame(cbind(var1, var2, var3, factor1, factor2, factor3)))
So factor1, factor2, and factor3 all end up numeric rather than factors. What am I missing? I’ve tried with and without as.data.frame and defining model_data as a dataframe before populating it.
My searches of the StackExchange archive return mostly results about deliberately changing factors to variables, and haven’t help me much. The slightly clunky title isto differentiate my question from those.
?cbindsays thatcbindreturns a matrix if all the inputs are vectors (which they are in your case). A a matrix can can only contain a single atomic type (character, numeric, logical, etc.). Factors are not an atomic type, so they get converted.The “Data frame methods” section says that
cbinddata.frame method just wrapsdata.frame(..., check.names=FALSE), so you could just calldata.framedirectly (the call tocbindis redundant).