对于具有多个因变量的重复测量,重塑与tidyr(2)

对于具有多个因变量的重复测量,重塑与tidyr(2),r,reshape2,melt,tidyr,R,Reshape2,Melt,Tidyr,我有以下5个案例的样本数据,其中对两个因变量“融洽关系”和“STRS”进行了三次重复测量: df1这是tidyr版本。您需要首先收集列,然后分离出时间列,最后分散数据 require(tidyr) df1 %>% gather(key, value, -c(SubID, Gender, Age)) %>% separate(key, c("key", "time")) %>% spread(key, value, convert = TRUE) ## Su

我有以下5个案例的样本数据,其中对两个因变量“融洽关系”和“STRS”进行了三次重复测量:


df1这是
tidyr
版本。您需要首先
收集
列,然后
分离
出时间列,最后
分散
数据

require(tidyr)
df1 %>% 
  gather(key, value, -c(SubID, Gender, Age)) %>% 
  separate(key, c("key", "time")) %>%
  spread(key, value, convert = TRUE)
##    SubID Gender Age time Rapport STRS
## 1      1      2   9    1    3.65   73
## 2      1      2   9    2    3.75   76
## 3      1      2   9    3    3.60   66
## 4      2      1  11    1    3.80  110
## 5      2      1  11    2    3.85  120
## 6      2      1  11    3    3.80  112
## 7      3      2  10    1    3.50  108
## 8      3      2  10    2    3.65  108
## 9      3      2  10    3    3.05  124
## 10     4      1  10    1    2.85  118
## 11     4      1  10    2    3.05  123
## 12     4      1  10    3    3.05  128
## 13     5      2  11    1    3.45  132
## 14     5      2  11    2    2.45  122
## 15     5      2  11    3    1.75  127

下面是一个使用

df2<-reshape(df1, varying = 4:9, sep = ".", direction = 'long')
View(df2)
df3<-melt(df1, id.vars=c("SubID","Gender","Age"),
measure.vars=c("Rapport.1","Rapport.2","Rapport.3","STRS.1","STRS.2","STRS.3,
variable.name=c("Rapport","STRS"),
value.name=("Rapport","STRS"))
df4<-gather(df1, Rapport, STRS, Rapport.1:STRS.3)
View(df4)
require(tidyr)
df1 %>% 
  gather(key, value, -c(SubID, Gender, Age)) %>% 
  separate(key, c("key", "time")) %>%
  spread(key, value, convert = TRUE)
##    SubID Gender Age time Rapport STRS
## 1      1      2   9    1    3.65   73
## 2      1      2   9    2    3.75   76
## 3      1      2   9    3    3.60   66
## 4      2      1  11    1    3.80  110
## 5      2      1  11    2    3.85  120
## 6      2      1  11    3    3.80  112
## 7      3      2  10    1    3.50  108
## 8      3      2  10    2    3.65  108
## 9      3      2  10    3    3.05  124
## 10     4      1  10    1    2.85  118
## 11     4      1  10    2    3.05  123
## 12     4      1  10    3    3.05  128
## 13     5      2  11    1    3.45  132
## 14     5      2  11    2    2.45  122
## 15     5      2  11    3    1.75  127
library(data.table) # v >= 1.9.5
melt(setDT(df1), measure = list(4:6, 7:9))
#     SubID Gender Age variable value1 value2
#  1:     1      2   9        1   3.65     73
#  2:     2      1  11        1    3.8    110
#  3:     3      2  10        1    3.5    108
#  4:     4      1  10        1   2.85    118
#  5:     5      2  11        1   3.45    132
#  6:     1      2   9        2   3.75     76
#  7:     2      1  11        2   3.85    120
#  8:     3      2  10        2   3.65    108
#  9:     4      1  10        2   3.05    123
# 10:     5      2  11        2   2.45    122
# 11:     1      2   9        3    3.6     66
# 12:     2      1  11        3    3.8    112
# 13:     3      2  10        3   3.05    124
# 14:     4      1  10        3   3.05    128
# 15:     5      2  11        3   1.75    127