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

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

问题描述

我知道 tidyr 包中的 spread 函数,但这是我无法实现的.我有一个带有 2 列的 data.frame,定义如下.我需要将列 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.

因为你只想要一个二元结果,我们可以先unique数据

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

<小时>

另一种使用 tidyrdplyr 的方法是

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天全站免登陆