【发布时间】:2016-06-10 08:51:06
【问题描述】:
我知道tidyr 包中的spread 函数,但这是我无法实现的。
我有一个data.frame,其中包含如下定义的 2 列。我需要将Subject 列转置为具有 1 和 0 的二进制列。
下面是数据框:
studentInfo <- data.frame(StudentID = c(1,1,1,2,3,3),
Subject = c("Maths", "Science", "English", "Maths", "History", "History"))
> studentInfo
StudentID Subject
1 1 Maths
2 1 Science
3 1 English
4 2 Maths
5 3 History
6 3 History
我期待的输出是:
StudentID Maths Science English History
1 1 1 1 1 0
2 2 1 0 0 0
3 3 0 0 0 1
如何使用spread() 函数或任何其他函数来做到这一点。
【问题讨论】: