简体   繁体   English

R串联两个因素

[英]R concatenating two factors

This is making me feel dumb, but I am trying to produce a single vector/df/list/etc (anything but a matrix) concatenating two factors. 这让我感到愚蠢,但是我试图产生一个将两个因素联系在一起的单个vector / df / list / etc(除矩阵之外的任何东西)。 Here's the scenario. 这是场景。 I have a 100k line dataset. 我有10万行数据集。 I used the top half to predict the bottom half and vice versa using knn . 我使用knn预测上半部分,反之亦然。 So now I have 2 objects created by knn predict() . 所以现在我有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"

Here's where my problem starts: 这是我的问题开始的地方:

> pred13 <- rbind(pred11, pred12)
> class(pred13)
[1] "matrix"

There are 2 problems. 有两个问题。 First it changes the 0's and 1's to 1's and 2's and second it seems to create a huge matrix that's eats all my memory. 首先,它将0和1更改为1和2,然后似乎创建了一个巨大的矩阵,吞噬了我所有的记忆。 I've tried messing with as.numeric() , data.frame() , etc, but can't get it to just combine the 2 50k factors into 1 100k one. 我曾尝试将as.numeric()data.frame()等弄乱,但无法将2个50k因子组合成1100k个。 Any suggestions? 有什么建议么?

@James presented one way, I'll chip in with another (shorter): @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

...This might seem a bit like magic, but unlist has special support for factors for this particular purpose! ...这似乎有点像魔术,但是出于这个特定目的, unlist对因素有特别的支持! All elements in the list must be factors for this to work. 列表中的所有元素必须是起作用的因素。

rbind will create 2 x 50000 matrix in your case which isn't what you want. rbind会根据您的情况创建2 x 50000矩阵,这不是您想要的。 c is the correct function to combine 2 vectors in a single longer vector. c是将2个向量合并为一个较长向量的正确函数。 When you use rbind or c on a factor, it will use the underlying integers that map to the levels. 在因子上使用rbindc时,它将使用映射到级别的基础整数。 In general you need to combine as a character before refactoring: 通常,您需要在重构之前将其组合为字符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM