通过ID和Days进行长期到广泛的重组

时间:2017-10-18 18:28:20

标签: r dplyr tidyr

假设:

x <- data.frame(Participant=c("1","1","1","1","1","1","1","1","1","1", "2","2","2","2","2","2","2","2","2","2",
                              "3","3","3","3","3","3","3","3","3","3"),
                Day= c("1","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10",
                       "1","2","3","4","5","6","7","8","9","10"),
                verbal=c(1,4,5,6,1,2,8,5,3,4,1,4,5,6,1,2,8,5,3,4,1,4,5,6,1,2,8,5,3,4),
                spatial=c(1,6,7,3,5,1,6,7,3,5,1,6,7,3,5,1,6,7,3,5,1,6,7,3,5,1,6,7,3,5),
                cognitive=c(5,6,3,5,7,1,2,3,6,1,6,7,3,5,1,6,7,3,5,1,6,7,3,5,7,4,2,5,7,4))

结果:

y <- data.frame(Participant=c("1","2","3"),
                verbal1=c(1,4,5),
                verbal2=c(1,4,5),
                verbal3=c(1,4,5),
                verbal4=c(1,4,5),
                verbal5=c(1,4,5),
                verbal6=c(1,4,5),
                verbal7=c(1,4,5),
                verbal8=c(1,4,5),
                verbal9=c(1,4,5),
                verbal10=c(1,4,5),
                spatial1=c(1,6,7),
                spatial2=c(1,6,7),
                spatial3=c(1,6,7),
                spatial4=c(1,6,7),
                spatial5=c(1,6,7),
                spatial6=c(1,6,7),
                spatial7=c(1,6,7),
                spatial8=c(1,6,7),
                spatial9=c(1,6,7),
                spatial10=c(1,6,7),
                cognitive1=c(5,6,3),
                cognitive2=c(5,6,3),
                cognitive3=c(5,6,3),
                cognitive4=c(5,6,3),
                cognitive5=c(5,6,3),
                cognitive6=c(5,6,3),
                cognitive7=c(5,6,3),
                cognitive8=c(5,6,3),
                cognitive9=c(5,6,3),
                cognitive10=c(5,6,3))

基本上,我很难将长格式数据集重组为宽格式。这里的问题是需要调用多个变量(语言,空间和认知),所以我不能用TidyR扩散函数来命中它。这里的诀窍是将其分解为评论部分中的三个部分:

解决方案:

x1 <- x %>%
  select(ParticipantID, Day, verbal) %>%
  mutate(Day=paste0('verbal', Day)) %>%
  spread(Day, verbal)


x2 <- x %>%
  select(ParticipantID, Day, spatial) %>%
  mutate(Day=paste0('spatial', Day)) %>%
  spread(Day, spatial)

x3 <- x %>%
  select(ParticipantID, Day, cognitive) %>%
  mutate(Day=paste0('cognitive', Day)) %>%
  spread(Day, cognitive)

final1 <- left_join(x1, x2, by=participant)
final2 <- left_join(x2,final1, by=participant)

1 个答案:

答案 0 :(得分:2)

如何将其分解为组成部分,然后重新加入,如下:

library(dplyr)
library(tidyr)

x1 <- select(x, Participant, Day, verbal) %>% mutate(Day = paste0('verbal',Day))
x2 <- select(x, Participant, Day, spatial) %>% mutate(Day = paste0('spatial',Day))
x3 <- select(x, Participant, Day, cognitive) %>% mutate(Day = paste0('cognitive',Day))

final <- left_join(left_join(spread(x1, Day, verbal), spread(x2, Day, spatial), by = 'Participant'), spread(x3, Day, cognitive), by = 'Participant')
final

# Participant verbal1 verbal10 verbal2 verbal3 verbal4 verbal5 verbal6 verbal7 verbal8 verbal9 spatial1 spatial10 spatial2 spatial3 spatial4 spatial5
# 1           1       1        4       4       5       6       1       2       8       5       3        1         5        6        7        3        5
# 2           2       1        4       4       5       6       1       2       8       5       3        1         5        6        7        3        5
# 3           3       1        4       4       5       6       1       2       8       5       3        1         5        6        7        3        5
# spatial6 spatial7 spatial8 spatial9 cognitive1 cognitive10 cognitive2 cognitive3 cognitive4 cognitive5 cognitive6 cognitive7 cognitive8 cognitive9
# 1        1        6        7        3          5           1          6          3          5          7          1          2          3          6
# 2        1        6        7        3          6           1          7          3          5          1          6          7          3          5
# 3        1        6        7        3          6           4          7          3          5          7          4          2          5          7