R - 将单列转换为多列的1和0

时间:2016-01-15 04:12:21

标签: r

我有一个如下所示的数据集:

data_original <- matrix(c("class1","class2","class3","class1","class2","class3","class1","class2","class3"),ncol=1,byrow=TRUE)
colnames(data_original) <- c("class")
rownames(data_original) <- c("student1","student2","student3","student1","student2","student3","student1","student2","student3")
data_original <- as.table(data_original)
data_original

         class 
student1 class1
student2 class2
student3 class3
student1 class1
student2 class2
student3 class3
student1 class1
student2 class2
student3 class3

我希望它看起来像这样:

data_req <- matrix(c(1,1,0,1,0,0,1,1,0),ncol=3,byrow=TRUE)
colnames(data_req) <- c("class1","class2","class3")
rownames(data_req) <- c("student1","student2","student3")
data_req <- as.table(data_req)
data_req

        class1 class2 class3
student1      1      1      0
student2      1      0      0
student3      1      1      0

基本上我想转换class列中的值,该值指示学生正在将哪个班级列入其自己的列。 是否有R包可以做到这一点?

3 个答案:

答案 0 :(得分:1)

如果您可以将数据转换为数据框,我认为这将更容易。

df <- data.frame(student=rownames(data_original), class=data_original[,1])

然后你可以使用

library(reshape2)
dcast(unique(df), student ~ class, length, value.var="class")

答案 1 :(得分:1)

给定所需的输出,似乎输入应该是如下(作为数据帧)

data_original <- structure(list(student = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L), .Label = c("student1", "student2", "student3"), class = "factor"), 
    class = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("class1", 
    "class2", "class3"), class = "factor"), val = c(1, 1, 1, 
    1, 0, 1, 0, 0, 0)), .Names = c("student", "class", "val"), row.names = c(NA, 
-9L), class = "data.frame")

以更易阅读的形式

   student  class val
1 student1 class1   1
2 student2 class1   1
3 student3 class1   1
4 student1 class2   1
5 student2 class2   0
6 student3 class2   1
7 student1 class3   0
8 student2 class3   0
9 student3 class3   0

tidyr解决方案如下

library(dplyr)
library(tidyr)

data_original %>% spread(class, val)

答案 2 :(得分:1)

我们可以使用xtabs

xtabs(val~student+class, data_original)
#             class
#student    class1 class2 class3
#  student1      1      1      0
#  student2      1      0      0
#  student3      1      1      0