我想知道您是否可以帮助我将数据转换为纵向格式。 我有一个数据集如下(20个变量):
ptno E0sex calpcvd calcvdea calrxgp age.baseline age.year1 age.year2 age.year3 age.year4 age.year5 age.year6 Baseline year1 year2 year3 year4 year5 year6 duration1
10021001 1 0 0 1 81.3744 82.3744 83.3744 84.3744 85.3744 86.3744 87.3744 0.66 0.68 0.64 0.64 0.38 8 8 6.5
10021002 1 0 0 2 81.18822 82.18822 83.18822 84.18822 85.18822 86.18822 87.18822 0.64 9 9 9 9 9 9 0.989041096
10021003 1 0 0 1 80.32307 81.32307 82.32307 83.32307 84.32307 85.32307 86.32307 0.63 8 9 9 9 9 9 1.21369863
10021004 1 0 0 1 87.00343 88.00343 89.00343 90.00343 91.00343 92.00343 93.00343 0.72 8 9 9 9 9 9 1.347945205
10021005 1 0 0 2 82.42026 83.42026 84.42026 85.42026 86.42026 87.42026 88.42026 0.74 8 8 8 8 8 9 5.052054795
10021006 1 0 0 2 81.54962 82.54962 83.54962 84.54962 85.54962 86.54962 87.54962 0.59 8 8 8 8 9 9 4.208219178
我想重新安排这一点(年龄全部合并为“年龄”和基线,第1年......合并为实用程序:
id obs E0sex calpcvd calcvdea calrxgp age utility duration
10021001 1 1 0 0 1 81.3744 0.66 6.5
10021001 2 1 0 0 1 82.3744 0.68 6.5
10021001 3 1 0 0 1 83.3744 0.64 6.5
10021001 4 1 0 0 1 84.3744 0.64 6.5
10021001 5 1 0 0 1 85.3744 0.38 6.5
10021001 6 1 0 0 1 86.3744 8 6.5
10021001 7 1 0 0 1 87.3744 8 6.5
10021002 1 1 0 0 2 81.18822 0.64 0.989041096
10021002 2 1 0 0 2 82.18822 9 0.989041096
10021002 3 1 0 0 2 83.18822 9 0.989041096
10021002 4 1 0 0 2 84.18822 9 0.989041096
10021002 5 1 0 0 2 85.18822 9 0.989041096
10021002 6 1 0 0 2 86.18822 9 0.989041096
10021002 7 1 0 0 2 87.18822 9 0.989041096
最好的方法是什么?
答案 0 :(得分:2)
假设您的数据是样本:
dput(sample)
structure(list(ptno = 10021001:10021006, E0sex = c(1L, 1L, 1L,
1L, 1L, 1L), calpcvd = c(0L, 0L, 0L, 0L, 0L, 0L), calcvdea = c(0L,
0L, 0L, 0L, 0L, 0L), calrxgp = c(1L, 2L, 1L, 1L, 2L, 2L), age.baseline = c(81.3744,
81.18822, 80.32307, 87.00343, 82.42026, 81.54962), age.year1 = c(82.3744,
82.18822, 81.32307, 88.00343, 83.42026, 82.54962), age.year2 = c(83.3744,
83.18822, 82.32307, 89.00343, 84.42026, 83.54962), age.year3 = c(84.3744,
84.18822, 83.32307, 90.00343, 85.42026, 84.54962), age.year4 = c(85.3744,
85.18822, 84.32307, 91.00343, 86.42026, 85.54962), age.year5 = c(86.3744,
86.18822, 85.32307, 92.00343, 87.42026, 86.54962), age.year6 = c(87.3744,
87.18822, 86.32307, 93.00343, 88.42026, 87.54962), Baseline = c(0.66,
0.64, 0.63, 0.72, 0.74, 0.59), year1 = c(0.68, 9, 8, 8, 8, 8),
year2 = c(0.64, 9, 9, 9, 8, 8), year3 = c(0.64, 9, 9, 9,
8, 8), year4 = c(0.38, 9, 9, 9, 8, 8), year5 = c(8L, 9L,
9L, 9L, 8L, 9L), year6 = c(8L, 9L, 9L, 9L, 9L, 9L), duration1 = c(6.5,
0.989041096, 1.21369863, 1.347945205, 5.052054795, 4.208219178
)), .Names = c("ptno", "E0sex", "calpcvd", "calcvdea", "calrxgp",
"age.baseline", "age.year1", "age.year2", "age.year3", "age.year4",
"age.year5", "age.year6", "Baseline", "year1", "year2", "year3",
"year4", "year5", "year6", "duration1"), class = "data.frame", row.names = c(NA,
-6L))
b<-reshape(sample,direction='long',varying=list(c(6,7,8,9,10,11,12),c(13,14,15,16,17,18,19)),v.names=c("age","utility"))
> b
ptno E0sex calpcvd calcvdea calrxgp duration1 time age utility id
1.1 10021001 1 0 0 1 6.5000000 1 81.37440 0.66 1
2.1 10021002 1 0 0 2 0.9890411 1 81.18822 0.64 2
3.1 10021003 1 0 0 1 1.2136986 1 80.32307 0.63 3
4.1 10021004 1 0 0 1 1.3479452 1 87.00343 0.72 4
5.1 10021005 1 0 0 2 5.0520548 1 82.42026 0.74 5
6.1 10021006 1 0 0 2 4.2082192 1 81.54962 0.59 6
1.2 10021001 1 0 0 1 6.5000000 2 82.37440 0.68 1
2.2 10021002 1 0 0 2 0.9890411 2 82.18822 9.00 2
3.2 10021003 1 0 0 1 1.2136986 2 81.32307 8.00 3
4.2 10021004 1 0 0 1 1.3479452 2 88.00343 8.00 4
5.2 10021005 1 0 0 2 5.0520548 2 83.42026 8.00 5
6.2 10021006 1 0 0 2 4.2082192 2 82.54962 8.00 6
1.3 10021001 1 0 0 1 6.5000000 3 83.37440 0.64 1
2.3 10021002 1 0 0 2 0.9890411 3 83.18822 9.00 2
3.3 10021003 1 0 0 1 1.2136986 3 82.32307 9.00 3
4.3 10021004 1 0 0 1 1.3479452 3 89.00343 9.00 4
5.3 10021005 1 0 0 2 5.0520548 3 84.42026 8.00 5
6.3 10021006 1 0 0 2 4.2082192 3 83.54962 8.00 6
1.4 10021001 1 0 0 1 6.5000000 4 84.37440 0.64 1
2.4 10021002 1 0 0 2 0.9890411 4 84.18822 9.00 2
3.4 10021003 1 0 0 1 1.2136986 4 83.32307 9.00 3
4.4 10021004 1 0 0 1 1.3479452 4 90.00343 9.00 4
5.4 10021005 1 0 0 2 5.0520548 4 85.42026 8.00 5
6.4 10021006 1 0 0 2 4.2082192 4 84.54962 8.00 6
1.5 10021001 1 0 0 1 6.5000000 5 85.37440 0.38 1
2.5 10021002 1 0 0 2 0.9890411 5 85.18822 9.00 2
3.5 10021003 1 0 0 1 1.2136986 5 84.32307 9.00 3
4.5 10021004 1 0 0 1 1.3479452 5 91.00343 9.00 4
5.5 10021005 1 0 0 2 5.0520548 5 86.42026 8.00 5
6.5 10021006 1 0 0 2 4.2082192 5 85.54962 8.00 6
1.6 10021001 1 0 0 1 6.5000000 6 86.37440 8.00 1
2.6 10021002 1 0 0 2 0.9890411 6 86.18822 9.00 2
3.6 10021003 1 0 0 1 1.2136986 6 85.32307 9.00 3
4.6 10021004 1 0 0 1 1.3479452 6 92.00343 9.00 4
5.6 10021005 1 0 0 2 5.0520548 6 87.42026 8.00 5
6.6 10021006 1 0 0 2 4.2082192 6 86.54962 9.00 6
1.7 10021001 1 0 0 1 6.5000000 7 87.37440 8.00 1
2.7 10021002 1 0 0 2 0.9890411 7 87.18822 9.00 2
3.7 10021003 1 0 0 1 1.2136986 7 86.32307 9.00 3
4.7 10021004 1 0 0 1 1.3479452 7 93.00343 9.00 4
5.7 10021005 1 0 0 2 5.0520548 7 88.42026 9.00 5
6.7 10021006 1 0 0 2 4.2082192 7 87.54962 9.00 6