我正在寻找有关如何循环以下内容的建议,这是一个更大的数据集的子集。我希望以下表示有效。
mydf <- structure(list(site_id = c("39ADA00070", "39ADA00070", "39ADA00070",
"39ADA00070", "39ADA00070", "39ADA00070", "39ADA00070", "39ADA00070",
"39ADA00070", "39ADA00070", "39ADA00070", "39ADA00070", "39ADA00070",
"39ADA00070", "39ADA00070", "39ADA00070", "39ADA00070", "39ADA00070",
"39ADA00070", "39ADA00070", "39ADA00070", "39ADA00070", "39ADA00070",
"39ADA00070", "39ADA00070", "39ADA00070", "39ADA00070", "39ADA00070",
"39ADA00070", "39ADA00070", "39ALL00184", "39ALL00184", "39ALL00184",
"39ALL00184", "39ALL00184", "39ALL00184", "39ALL00184", "39ALL00184",
"39ALL00184", "39ALL00184", "39ALL00184", "39ALL00184", "39ALL00184",
"39ALL00184", "39ALL00184", "39ALL00184", "39ALL00184", "39ALL00184",
"39ALL00184", "39ALL00184", "39ALL00184", "39ALL00184", "39ALL00184"
), date = structure(c(6339, 8594, 9293, 9441, 10014, 10604, 11080,
11821, 12717, 12907, 13081, 13277, 13459, 13635, 13822, 14012,
14207, 14207, 14355, 14564, 14704, 14917, 15105, 15271, 15478,
15644, 15833, 15834, 16009, 16203, 7783, 8406, 8554, 8686, 9034,
9260, 9632, 9777, 10002, 10491, 10491, 11060, 11585, 12145, 12145,
12696, 13242, 13242, 13775, 14363, 14881, 15428, 15974), class = "Date"),
var1 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 159L, 148L,
149L, 134L, 179L, 205L, 193L, 109L, 109L, 177L, 75L, 272L,
150L, 115L, 232L, 230L, 183L, 159L, 159L, 304L, 220L, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
-98L, -98L, -38L, -74L, -74L, -80L, -48L), var2 = c(NA, NA,
NA, NA, NA, NA, NA, NA, 16.8, 16.8, 14.5, 14.2, 15.1, 14.5,
15, 15.2, 13.2, 13.2, 15, 15.2, 15.1, 14.4, 14.8, 15.2, 16.3,
NA, 14.3, 14.3, 15.6, 14.8, NA, 12, 14.7, NA, 14.6, NA, 13.7,
12.3, 12.5, 13.5, 13.5, 12.5, 13.1, 14.2, 14.2, 14.1, 12.5,
12.5, 13.5, 12.7, 12.6, 12.5, 12.6), var3 = c(NA, NA, NA,
NA, NA, NA, NA, NA, 7.35, 7.85, 7.5, 7.47, 7.62, 7.08, 7.08,
7.2, 7.4, 7.4, 7.26, 7.05, 6.56, 7.2, 7.42, 6.5, 7.81, 8.43,
7.57, 7.57, 7.42, 7.72, NA, 6.58, 6.8, NA, 7.75, NA, 7.06,
6.77, 6.41, 6.84, 6.84, 7.85, 7.13, 7.26, 7.26, 7.06, 7.14,
7.14, 7.11, 6.9, 7.11, 7.2, 7.1), var4 = c(NA, 283L, 216L,
223L, 256L, 165L, 192L, 216L, 173L, 216L, 179L, 282L, 146L,
227L, 141L, 210L, 160L, 162L, 157L, 140L, 235L, 166L, 216L,
NA, 162L, 193L, 286L, 274L, 163L, 209L, NA, 304L, 321L, 293L,
398L, 302L, 301L, 282L, 288L, 292L, 292L, 302L, 515L, 309L,
309L, 323L, 338L, 295L, 280L, 279L, 325L, 328L, 322L), var5 = c(NA,
NA, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), var6 = c(NA, NA,
29L, 32L, 36L, 24L, 25L, 29L, 27L, 27L, 24L, 32L, 21L, 27L,
21L, 26L, 23L, 24L, 25L, 20L, 24L, 22L, 28L, 24L, 20L, 23L,
30L, 29L, 21L, 24L, 15L, 15L, 18L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L), var7 = c(NA, NA, 77, 83, 87, 66, 73, 73,
65, 76, 69, 93, 60, 76, 56, 77, 67, 68, 68, 60, 67, 63, 82,
69, 56, 68, 85, 83, 59, 68.2, 157, 159, 164, 169, 155, 176,
156, 156, 162, 162, 162, 160, 180, 163, 163, 158, 168, 171,
162, 167, 177, 167, 168), var8 = c(NA, NA, 25, 26, 29, 21,
22, 23, 20, 23, 21, 30, 17, 24, 16, 23, 20, 20, 21, 17, 23,
18, 25, 20, 17, 21, 27, 27, 17, 20.9, 91, 89, 96, 92, 86,
100, 89, 91, 92, 94, 94, 91, 97, 91, 91, 92, 98, 99, 94,
100, 106, 98, 100), var9 = c(1.02, 1, 0.37, 0.48, 0.88, 0.16,
0.17, 0.24, 0.25, 5.98, 0.26, 0.54, 0, 0.19, 0, 0.18, 0.14,
0.13, 0.16, 0.11, 0.19, 0.16, 0.26, NA, 0.11, 0.27, 0.19,
0.19, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, NA, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0, 0, 0.1, 0.1,
0.1), var10 = c(50, 48, 64, 55, 52, 64, 69, 63.3, 56.1, 40.6,
58.6, 43.9, 62.2, 51.9, 55.6, 53.4, 61.3, 61, 61.1, 61.9,
51.5, 60.7, 52.2, NA, 66, 52.8, 46.8, 47.5, 59.2, 53.4, NA,
560, 650, 540, 548, 655, 565, 531, 540, 501, 501, 531, 535,
547, 547, 492, 537, 542, 512, 542, 548, 581, 540)), class = "data.frame", row.names = c(NA,
-53L), .Names = c("site_id", "date", "var1", "var2", "var3",
"var4", "var5", "var6", "var7", "var8", "var9", "var10"))
此data.frame
是一组不规则的时间序列,使用site_id
作为主要ID因子,date
作为日期,然后使用10个变量。实际data.frame
有数百个ID和数十个因素。
我知道我可以site_id
使用,例如
mydf[mydf$site_id == '39ADA00070', ][,3]
获取第一个var1
的{{1}}。
我正在寻找的是一个强大的site_id
循环来贯穿for
:
data.frame
这可能是任何数量的测试或情节,例如
for (i in 1:length(site_id)){
perform something on
var1 through var10
output matrix of that something
}
(来自GetOutliers()
包)
来自extremevalues
的各种情节
等等。
但首先,我需要帮助正确分配ggplot2
循环索引。
我并不反对使用for
(apply
)工具来完成这项工作,但我想从一个基本的ddply
循环开始。然后我可以通过解决for
s,审查值等来清理。
非常感谢!
答案 0 :(得分:0)
请尝试以下操作:
for(ss in unique(mydf$site_id)){
for(cc in 3:12){
# do whatever function
print(max(mydf[mydf$site_id == ss, cc],na.rm=TRUE))
}
}
[1] 304
[1] 16.8
[1] 8.43
[1] 286
[1] 2
[1] 36
[1] 93
[1] 30
[1] 5.98
[1] 69
[1] -38
[1] 14.7
[1] 7.85
[1] 515
[1] 2
[1] 18
[1] 180
[1] 106
[1] 0.1
[1] 655