[英]Wide to long: multiple columns, two timepoints, two groups
I have searched and found a number of examples, so far I have not been able to solve a problem in transforming my data from wide to long.我搜索并找到了许多示例,到目前为止,我还无法解决将数据从宽转换为长的问题。
Below is an example of the data:以下是数据示例:
set.seed(12345)
id = 1:100
age = sample(1:100, 100, replace=TRUE)
group = sample(1:2, 100, replace=TRUE)
t0_var1 = sample(1:300, 100, replace=TRUE)
t2_var1 = sample(1:300, 100, replace=TRUE)
t0_var2 = sample(1:600, 100, replace=TRUE)
t2_var2 = sample(1:600, 100, replace=TRUE)
t0_var3 = sample(1:700, 100, replace=TRUE)
t2_var3 = sample(1:700, 100, replace=TRUE)
dataset = data.frame(id, age, group, t0_var1, t2_var1, t0_var2, t2_var2, t0_var3, t2_var3)
head(dataset)
id age group t0_var1 t2_var1 t0_var2 t2_var2 t0_var3 t2_var3
1 1 73 1 177 16 46 126 490 431
2 2 88 2 268 180 285 460 8 250
3 3 77 2 38 213 159 505 353 326
4 4 89 2 154 197 139 561 512 210
5 5 46 2 200 174 358 406 231 50
6 6 17 2 230 204 96 516 575 506
I need the columns to be arranged as follows:我需要按如下方式排列列:
id | age | group | assessment | var1 | var2 | var3
1 | 23 1 1 12 34 64
1 | 23 1 2 34 14 68
2 | 43 2 1 22 44 54
2 | 43 2 2 34 54 88
...
I tried in different ways, but I'm only managing to transform two columns at a time.我尝试了不同的方式,但我一次只能转换两列。
Thanks in advance for your help.在此先感谢您的帮助。
From Ben's comment:来自本的评论:
pivot_longer(dataset, cols = -c(id, age, group), names_to = c("assessment", ".value"), names_pattern = "t(\\d+)_(\\w+)")
# A tibble: 200 x 7
id age group assessment var1 var2 var3
<int> <int> <int> <chr> <int> <int> <int>
1 1 14 1 0 287 1 280
2 1 14 1 2 266 5 523
3 2 51 2 0 136 456 444
4 2 51 2 2 66 292 260
5 3 80 1 0 111 263 635
6 3 80 1 2 275 255 70
7 4 90 1 0 174 438 212
8 4 90 1 2 161 161 694
9 5 92 1 0 86 184 595
10 5 92 1 2 176 399 32
The regular expression t(\\d+)_(\\w+)
means:正则表达式t(\\d+)_(\\w+)
表示:
Thus the return values for this problem will be the the number following the "t" (ex. 0 or 2) and then the suffix of the column name (ex. var1, var2, var3)因此,此问题的返回值将是“t”后面的数字(例如 0 或 2),然后是列名的后缀(例如 var1、var2、var3)
We can use melt
from data.table
我们可以使用data.table
的melt
library(data.table)
melt(setDT(dataset), measure = patterns("var1$", "var2$", "var3$"),
value.name = paste0("var", 1:3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.