将因子向量转换为R中具有二进制变量的列

时间:2014-02-02 04:44:41

标签: r

以前可能会问过类似的问题,但我找不到确切的情况。我有一些数据,比如说,

> t <- data.frame(project=c("A","A","B","C","C"),lang=factor(c("X","Y","X","Y","Z")))
> t
  project lang
1       A    X
2       A    Y
3       B    X
4       C    Y
5       C    Z

我想把它变成以下

  project X Y Z
1       A 1 1 0
2       B 1 0 0
3       C 0 1 1

也就是说,如果lang中的1行{{}},我想将i中的每个因子级别转换为值t的列1}}具有该级别,否则为lang[i]。我可以将它与一些for循环和诸如此类的东西一起破解,但是有更简单的方法吗?

1 个答案:

答案 0 :(得分:4)

始终有table

> table(t)
       lang
project X Y Z
      A 1 1 0
      B 1 0 0
      C 0 1 1

根据您想要的重复行为(因此制表会产生大于1的数字),您可能需要执行额外的步骤:

## New sample data with a duplicated "A, X" combination
t <- data.frame(project=c("A","A","A", "B","C","C"),
                lang=factor(c("X","Y","X", "X","Y","Z")))

out <- table(t)
out
#        lang
# project X Y Z
#       A 2 1 0
#       B 1 0 0
#       C 0 1 1
out[out >= 1] <- 1
out
#        lang
# project X Y Z
#       A 1 1 0
#       B 1 0 0
#       C 0 1 1