将多个布尔列转换为单因子列

时间:2014-06-23 08:07:09

标签: r matrix multiple-columns

我的数据框看起来像这样:

      A S1 S2 S3 S4
1   ex1  1  0  0  0
2   ex2  0  1  0  0
3   ex3  0  0  1  0
4   ex4  1  0  0  0
5   ex5  0  0  0  1
6   ex6  0  1  0  0
7   ex7  1  0  0  0
8   ex8  0  1  0  0
9   ex9  0  0  1  0
10 ex10  1  0  0  0

我需要将它作为单个因素列表,如:

A   Type
ex1 S1
ex2 S2
ex3 S3
ex4 S1
ex5 S4
ex6 S2
ex7 S1
ex8 S2
ex9 S3
ex10 S1

有人帮我解决问题吗?

4 个答案:

答案 0 :(得分:2)

您可以使用apply并检查列2-5中的最大值,然后返回相应的列名称:

df$Type <- apply(df[2:5], 1, function(x) names(df)[which.max(x)+1] )

之后,您可以删除不再需要的列:

df <- df[,-c(2:5)]

答案 1 :(得分:2)

也可以(如果dat是您的数据集)

library(reshape2)
dat <- melt(dat, id = "A")
dat[dat$value > 0, 1:2]

答案 2 :(得分:2)

假设d是数据,可以使用

获取新列
d$type <- names(d[-1])[apply(d[-1] == 1, 1, which)]
d[c(1, 6)]
#       A type
# 1   ex1   S1
# 2   ex2   S2
# 3   ex3   S3
# 4   ex4   S1
# 5   ex5   S4
# 6   ex6   S2
# 7   ex7   S1
# 8   ex8   S2
# 9   ex9   S3
# 10 ex10   S1

答案 3 :(得分:0)

您可以尝试: 如果df是数据框

data.frame(A=df$A, Type=rep(names(df)[-1], nrow(df))[!!t(df[,-1])])
    A Type
1   ex1   S1
2   ex2   S2
3   ex3   S3
4   ex4   S1
5   ex5   S4
6   ex6   S2
7   ex7   S1
8   ex8   S2
9   ex9   S3
10 ex10   S1

此外:

   names(df)[-1][t(df[-1])*seq_len(ncol(df)-1)]
 [1] "S1" "S2" "S3" "S1" "S4" "S2" "S1" "S2" "S3" "S1"