从长到宽整形并创建具有二进制值的列 [英] Reshape from long to wide and create columns with binary value

查看:59
本文介绍了从长到宽整形并创建具有二进制值的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 tidyr 包中的价差函数,但这是我无法实现的。
我有一个 data.frame ,其中有2列,定义如下。我需要将列 Subject 转换为具有1和0的二进制列。

I am aware of the spread function in the tidyr package but this is something I am unable to achieve. I have a data.frame with 2 columns as defined below. I need to transpose the column Subject into binary columns with 1 and 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()函数或任何其他方法执行此操作

How can I do this with the spread() function or any other function.

推荐答案

使用 reshape2 我们可以 dcast 从长到宽。

Using reshape2 we can dcast from long to wide.

由于只需要二进制结果,我们可以唯一数据优先

As you only want a binary outcome we can unique the data first

library(reshape2)

si <- unique(studentInfo)
dcast(si, formula = StudentID ~ Subject, fun.aggregate = length)

#  StudentID English History Maths Science
#1         1       1       0     1       1
#2         2       0       0     1       0
#3         3       0       1     0       0






另一种使用 tidyr dplyr的方法

library(tidyr)
library(dplyr)

studentInfo %>%
  mutate(yesno = 1) %>%
  distinct %>%
  spread(Subject, yesno, fill = 0)

#  StudentID English History Maths Science
#1         1       1       0     1       1
#2         2       0       0     1       0
#3         3       0       1     0       0

尽管我还不是 tidyr 语法的粉丝...

Although I'm not a fan (yet) of tidyr syntax...

这篇关于从长到宽整形并创建具有二进制值的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆