我有以下data.frame(追加):
我希望绘制的是每个变量的值的纵向变化,其中日期由Name表示。但是,我首先要对每个值进行标准化,使其对于每个变量/名称组合的第一个数据点等于1。
我可以使用
找到每个名字的最短日期aggregate(df.m[, c("Date")], list(df.m$Name), min)
我可以使用ddply
包中的plyr
来应用更简单的比例。例如。根据每个名称/变量组合的最大值缩放每个值:
ddply(df.m, .(Name,variable), transform, norm.ef = value/max(value))
像
这样的东西ddply(df.m, .(Name,variable), transform, norm.ef = value/min_date(value))
我该如何做类似的事情 - 根据最小日期的值缩放值
structure(list(Name = c("Z7", "Z8", "Z7", "Z10", "Z6", "Z4",
"Z3", "Z2", "Z1", "Z9", "Z7", "Z10", "Z8", "Z6", "Z3", "Z6",
"Z6", "Z2", "Z4", "Z5", "Z9", "Z2", "Z5", "Z8", "Z3", "Z3", "Z3",
"Z10", "Z10", "Z10", "Z1", "Z5", "Z7", "Z7", "Z10", "Z2", "Z2",
"Z3", "Z8", "Z6", "Z3", "Z2", "Z7", "Z8", "Z9", "Z3", "Z7", "Z1",
"Z5", "Z2", "Z4", "Z5", "Z7", "Z7", "Z6", "Z9", "Z1", "Z4", "Z8",
"Z5", "Z4", "Z7", "Z1", "Z9", "Z6", "Z7", "Z8", "Z7", "Z10",
"Z6", "Z4", "Z3", "Z2", "Z1", "Z9", "Z7", "Z10", "Z8", "Z6",
"Z3", "Z6", "Z6", "Z2", "Z4", "Z5", "Z9", "Z2", "Z5", "Z8", "Z3",
"Z3", "Z3", "Z10", "Z10", "Z10", "Z1", "Z5", "Z7", "Z7", "Z10",
"Z2", "Z2", "Z3", "Z8", "Z6", "Z3", "Z2", "Z7", "Z8", "Z9", "Z3",
"Z7", "Z1", "Z5", "Z2", "Z4", "Z5", "Z7", "Z7", "Z6", "Z9", "Z1",
"Z4", "Z8", "Z5", "Z4", "Z7", "Z1", "Z9", "Z6", "Z7", "Z8", "Z7",
"Z10", "Z6", "Z4", "Z3", "Z2", "Z1", "Z9", "Z7", "Z10", "Z8",
"Z6", "Z3", "Z6", "Z6", "Z2", "Z4", "Z5", "Z9", "Z2", "Z5", "Z8",
"Z3", "Z3", "Z3", "Z10", "Z10", "Z10", "Z1", "Z5", "Z7", "Z7",
"Z10", "Z2", "Z2", "Z3", "Z8", "Z6", "Z3", "Z2", "Z7", "Z8",
"Z9", "Z3", "Z7", "Z1", "Z5", "Z2", "Z4", "Z5", "Z7", "Z7", "Z6",
"Z9", "Z1", "Z4", "Z8", "Z5", "Z4", "Z7", "Z1", "Z9", "Z6", "Z7",
"Z8", "Z7", "Z10", "Z6", "Z4", "Z3", "Z2", "Z1", "Z9", "Z7",
"Z10", "Z8", "Z6", "Z3", "Z6", "Z6", "Z2", "Z4", "Z5", "Z9",
"Z2", "Z5", "Z8", "Z3", "Z3", "Z3", "Z10", "Z10", "Z10", "Z1",
"Z5", "Z7", "Z7", "Z10", "Z2", "Z2", "Z3", "Z8", "Z6", "Z3",
"Z2", "Z7", "Z8", "Z9", "Z3", "Z7", "Z1", "Z5", "Z2", "Z4", "Z5",
"Z7", "Z7", "Z6", "Z9", "Z1", "Z4", "Z8", "Z5", "Z4", "Z7", "Z1",
"Z9", "Z6"), Date = structure(c(13601, 13889, 13195, 13440, 13643,
13833, 13223, 13160, 13846, 13924, 13699, 13706, 13797, 13714,
13461, 13818, 13818, 13531, 13938, 13902, 13524, 13692, 13706,
13643, 13377, 13377, 13314, 13804, 13973, 13973, 13468, 13797,
13377, 12950, 13896, 13412, 13412, 13608, 13720, 13924, 12971,
13776, 13405, 13748, 13748, 13608, 13496, 13671, 13388, 13629,
13622, 13987, 12984, 12984, 13454, 13986, 13776, 14029, 14176,
13622, 14084, 13293, 13937, 13846, 14028, 13601, 13889, 13195,
13440, 13643, 13833, 13223, 13160, 13846, 13924, 13699, 13706,
13797, 13714, 13461, 13818, 13818, 13531, 13938, 13902, 13524,
13692, 13706, 13643, 13377, 13377, 13314, 13804, 13973, 13973,
13468, 13797, 13377, 12950, 13896, 13412, 13412, 13608, 13720,
13924, 12971, 13776, 13405, 13748, 13748, 13608, 13496, 13671,
13388, 13629, 13622, 13987, 12984, 12984, 13454, 13986, 13776,
14029, 14176, 13622, 14084, 13293, 13937, 13846, 14028, 13601,
13889, 13195, 13440, 13643, 13833, 13223, 13160, 13846, 13924,
13699, 13706, 13797, 13714, 13461, 13818, 13818, 13531, 13938,
13902, 13524, 13692, 13706, 13643, 13377, 13377, 13314, 13804,
13973, 13973, 13468, 13797, 13377, 12950, 13896, 13412, 13412,
13608, 13720, 13924, 12971, 13776, 13405, 13748, 13748, 13608,
13496, 13671, 13388, 13629, 13622, 13987, 12984, 12984, 13454,
13986, 13776, 14029, 14176, 13622, 14084, 13293, 13937, 13846,
14028, 13601, 13889, 13195, 13440, 13643, 13833, 13223, 13160,
13846, 13924, 13699, 13706, 13797, 13714, 13461, 13818, 13818,
13531, 13938, 13902, 13524, 13692, 13706, 13643, 13377, 13377,
13314, 13804, 13973, 13973, 13468, 13797, 13377, 12950, 13896,
13412, 13412, 13608, 13720, 13924, 12971, 13776, 13405, 13748,
13748, 13608, 13496, 13671, 13388, 13629, 13622, 13987, 12984,
12984, 13454, 13986, 13776, 14029, 14176, 13622, 14084, 13293,
13937, 13846, 14028), class = "Date"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L), .Label = c("single_ef", "dual_ef", "wig_ef", "lightbox_ef"
), class = "factor"), value = c(33.35, 34.325, 42.35, 79.825,
41.85, 38.25, 36.875, 47.8, 55, 45.15, 49.8, 102.95, 32.85, 36.45,
33.675, 39.025, 38.25, 42.9, 28.7, 49.625, 55.875, 43.7, 44.8,
52.65, 32.8, 34.45, 35.45, 52.325, 102.25, 67.475, 60.75, 43.05,
40.975, 37.55, 57.125, 40.625, 47.425, 35.675, 38.05, 32.3, 39.825,
40.8, 42.3, 35.725, 61.875, 35.3, 45.3, 58.65, 45.225, 36.9,
47.275, 41.7, 43.925, 43.85, 40.375, 47.625, 41.875, 38.2, 40.7,
49.75, 31.4, 47.675, 55.3, 46.3, 36.2, 50, 60, 52, 77, 64, 67,
67, 61, 65, 55, 62, 81, 61, 60, 60, 59, 58, 64, 49, 64, 65, 64,
65, 58, 54, 56, 50, 59, 62, 57, 70, 73, 53, 52, 56, 57, 61, 55,
50, 49, 66, 57, 59, 52, 68, 55, 59, 74, 66, 55, 71, 64, 51, 58,
63, 63, 61, 58, 61, 65, 54, 64, 62, 62, 61, 49.4266, 57.9047,
51.9328, 76.2938, 62.7984, 65.1656, 64.9469, 61.5281, 62.9422,
54.3281, 61.7797, 100.475, 59.2, 58.5484, 60.1359, 57.4656, 56.4109,
61.5109, 49.3187, 63.1813, 63.9047, 63.0062, 64.0922, 55.5734,
53.8094, 54.6391, 47.6687, 56.4062, 96.925, 62.6, 70.1484, 72.3578,
52.3344, 51.1422, 58.9, 65.3328, 60.0781, 52.95, 47.9531, 48.675,
64.9531, 57.05, 57.5969, 50.3031, 66.4578, 53.1609, 58.0312,
71.6594, 66.6969, 54.0844, 70.7969, 63.025, 51.5047, 55.8719,
61.8406, 61.1484, 61.9953, 56.3891, 59.8453, 64.125, 54.1625,
60.9953, 60.5547, 60.3234, 59.9391, 50.8, 56.7, 62.2, 69.6, 74.2,
74.9, 72.2, 74.9, 72.8, 63.3, 63.1, 71.9, 55.7, 68.6, 69.6, 60.7,
60.7, 65.5, 64.8, 72, 79.4, 62.7, 65.4, 66, 65.7, 65.7, 57.6,
60.9, 67, 67, 78.9, 73, 47.2, 56.1, 65, 67.7, 67.7, 51.7, 42.7,
56, 75.5, 64.5, 56.1, 48, 81.5, 51.7, 58.4, 73.1, 72.2, 58.3,
75, 73.6, 59.7, 59.7, 72.3, 71.3, 73.7, 64, 60, 74.9, 64.9, 65.5,
61.6, 69.7, 60.5)), .Names = c("Name", "Date", "variable", "value"
), row.names = c(NA, -260L), class = "data.frame")
答案 0 :(得分:1)
在函数ddply()
子集值Date==min(Date)
中。这将选择与最小value
对应的Date
。
ddply(df.m, .(Name,variable), transform, norm.ef = value/value[Date==min(Date)])