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.
这会创建一个“original_data”副本,其中包含剩余因素。
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.
因此factor1,factor2和factor3都是数字而不是因素。我错过了什么?我尝试使用和不使用as.data.frame并在填充之前将model_data定义为数据帧。
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.
我对StackExchange归档文件的搜索主要是关于故意改变变量因素的结果,并且对我没什么帮助。略显笨重的标题是将我的问题与那些问题区分开来。
1 个解决方案
#1
1
?cbind
says that cbind
returns 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.
?cbind说如果所有输入都是向量(在你的情况下它们就是这样),cbind会返回一个矩阵。矩阵只能包含单个原子类型(字符,数字,逻辑等)。因素不是原子类型,因此它们会被转换。
The "Data frame methods" section says that cbind
data.frame method just wraps data.frame(..., check.names=FALSE)
, so you could just call data.frame
directly (the call to cbind
is redundant).
“数据框架方法”部分说cbind data.frame方法只包装data.frame(...,check.names = FALSE),所以你可以直接调用data.frame(对cbind的调用是多余的)。
model_data <- with(subset(copy_data, copy_data$var1<0),
data.frame(var1, var2, var3, factor1, factor2, factor3))
#1
1
?cbind
says that cbind
returns 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.
?cbind说如果所有输入都是向量(在你的情况下它们就是这样),cbind会返回一个矩阵。矩阵只能包含单个原子类型(字符,数字,逻辑等)。因素不是原子类型,因此它们会被转换。
The "Data frame methods" section says that cbind
data.frame method just wraps data.frame(..., check.names=FALSE)
, so you could just call data.frame
directly (the call to cbind
is redundant).
“数据框架方法”部分说cbind data.frame方法只包装data.frame(...,check.names = FALSE),所以你可以直接调用data.frame(对cbind的调用是多余的)。
model_data <- with(subset(copy_data, copy_data$var1<0),
data.frame(var1, var2, var3, factor1, factor2, factor3))