如何将多个变量的重复测量扩展为宽格式? [英] How can I spread repeated measures of multiple variables into wide format?

查看:45
本文介绍了如何将多个变量的重复测量扩展为宽格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试采用长格式的列并将它们展开为宽格式,如下所示.我想使用 tidyr 通过我投资的数据操作工具解决这个问题,但为了使这个答案更通用,请提供其他解决方案.

I'm trying to take columns that are in long format and spread them to wide format as shown below. I'd like to use tidyr to solve this with the data manipulation tools I'm investing in but to make this answer more general please provide other solutions.

这是我所拥有的:

library(dplyr); library(tidyr)

set.seed(10)
dat <- data_frame(
    Person = rep(c("greg", "sally", "sue"), each=2),
    Time = rep(c("Pre", "Post"), 3),
    Score1 = round(rnorm(6, mean = 80, sd=4), 0),
    Score2 = round(jitter(Score1, 15), 0),
    Score3 = 5 + (Score1 + Score2)/2
)

##   Person Time Score1 Score2 Score3
## 1   greg  Pre     80     78   84.0
## 2   greg Post     79     80   84.5
## 3  sally  Pre     75     74   79.5
## 4  sally Post     78     78   83.0
## 5    sue  Pre     81     78   84.5
## 6    sue Post     82     81   86.5

所需的宽格式:

  Person Pre.Score1 Pre.Score2 Pre.Score3  Post.Score1 Post.Score2 Post.Score3
1   greg         80         78       84.0           79          80        84.5
2  sally         75         74       79.5           78          78        83.0
3    sue         81         78       84.5           82          81        86.5

我可以通过为每个分数做这样的事情来做到这一点:

I can do it by doing something like this for each score:

spread(dat %>% select(Person, Time, Score1), Time, Score1) %>% 
    rename(Score1_Pre = Pre, Score1_Post = Post)

然后使用 _join 但这似乎很冗长,而且必须有更好的方法.

And then using _join but that seems verbose and like there's got to be a better way.

相关问题:
tidyr 宽到长有两个重复测量
是否有可能在 tidyr 中使用类似于 dcast 的多列传播?

推荐答案

如果你想坚持使用 tidyr/dplyr

dat %>% 
  gather(temp, score, starts_with("Score")) %>% 
  unite(temp1, Time, temp, sep = ".") %>% 
  spread(temp1, score)

这篇关于如何将多个变量的重复测量扩展为宽格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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