这让我感到愚蠢,但我试图产生一个矢量/ df/list/etc(除矩阵之外的任何东西)连接两个因素.这是场景.我有一个100k线数据集.我使用上半部分预测下半部分,反之亦然knn
.所以现在我创建了2个对象knn predict()
.
> head(pred11)
[1] 0 0 0 0 0 0
Levels: 0 1
> head(pred12)
[1] 0 1 1 0 0 0
Levels: 0 1
> class(pred11)
[1] "factor"
> class(pred12)
[1] "factor"
Run Code Online (Sandbox Code Playgroud)
这是我的问题开始的地方:
> pred13 <- rbind(pred11, pred12)
> class(pred13)
[1] "matrix"
Run Code Online (Sandbox Code Playgroud)
有两个问题.首先,它将0和1改为1和2,然后是第二,它似乎创造了一个巨大的矩阵,它占据了我所有的记忆.我已经试过搞乱as.numeric()
,data.frame()
等,但不能让它在2个50K因素只是合并为1 100K之一.有什么建议?
Tom*_*mmy 28
@James提出了一种方式,我会用另一种方式(更短):
set.seed(42)
x1 <- factor(sample(0:1,10,replace=T))
x2 <- factor(sample(0:1,10,replace=T))
unlist(list(x1,x2))
# [1] 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 0 0 1
#Levels: 0 1
Run Code Online (Sandbox Code Playgroud)
......这看起来有点神奇,但是unlist
对于这个特殊目的的因素有特别的支持!列表中的所有元素都必须是使其起作用的因素.
Jam*_*mes 18
rbind
将在您的情况下创建2 x 50000矩阵,这不是您想要的.c
是在单个较长向量中组合2个向量的正确函数.当您使用rbind
或c
使用因子时,它将使用映射到级别的基础整数.通常,您需要在重构之前将其组合为一个字符:
x1 <- factor(sample(0:1,10,replace=T))
x2 <- factor(sample(0:1,10,replace=T))
factor(c(as.character(x1),as.character(x2)))
[1] 1 1 1 0 1 1 0 1 0 0 0 1 1 1 1 1 1 0 0 0
Levels: 0 1
Run Code Online (Sandbox Code Playgroud)