用于循环和意外结果

时间:2016-12-02 10:10:56

标签: r for-loop

我试图写一个for循环,但我无法完成它。如果我将它们用于for循环,它运行良好,但我不知道问题出在哪里。

output100 <- structure(list(row = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), col = c(17L, 17L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 16L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L), cell = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), xcoord = c(783750L, 
783750L, 783750L, 783750L, 783750L, 783750L, 783750L, 783750L, 
783750L, 783750L, 783725L, 783725L, 783725L, 783725L, 783725L, 
783725L, 783725L, 783725L, 783725L, 783725L), ycoord = c(187050L, 
187050L, 187050L, 187050L, 187050L, 187050L, 187050L, 187050L, 
187050L, 187050L, 187025L, 187025L, 187025L, 187025L, 187025L, 
187025L, 187025L, 187025L, 187025L, 187025L), species = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("abiealba", "alnuviri", "larideci", "piceabie", 
"pinucemb", "pinusilv", "popunigr", "poputrem", "salicapr", "sorbaucu"
), class = "factor"), age = c(590L, 250L, 230L, 210L, 200L, 190L, 
180L, 110L, 100L, 90L, 720L, 320L, 300L, 230L, 170L, 160L, 150L, 
140L, 130L, 80L), biomass = c(6.3836, 1.2988, 0.9683, 0.6574, 
0.5083, 0.3398, 0.2163, 0.0863, 0.0591, 0.0418, 6.6135, 1.7666, 
1.214, 0.7032, 0.3422, 0.2571, 0.1601, 0.0846, 0.0592, 0.0323
), stems = c(1L, 1L, 3L, 1L, 2L, 6L, 5L, 8L, 3L, 5L, 1L, 3L, 
1L, 1L, 2L, 5L, 7L, 4L, 6L, 5L), slowGrowth = c(0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), DBH = c(104.9563, 50.7341, 44.7226, 37.9815, 34.1311, 28.9447, 
24.1329, 16.8379, 14.5727, 12.7875, 106.7731, 58.0343, 49.2757, 
39.0663, 29.027, 25.8599, 21.4205, 16.7129, 14.5803, 11.6105), 
    height = c(45.999, 30.659, 28.1508, 25.0823, 23.1987, 20.5, 
    17.8196, 13.4049, 11.9423, 10.7572, 46.3418, 33.4408, 30.0693, 
    25.5954, 20.5444, 18.802, 16.2293, 13.3254, 11.9472, 9.96
    ), availableLight = c(0.8129, 0.4994, 0.3701, 0.2541, 0.217, 
    0.1588, 0.102, 0.075, 0.06, 0.0545, 0.8083, 0.4101, 0.2332, 
    0.196, 0.1694, 0.1347, 0.0941, 0.0702, 0.0602, 0.0519), light_rf = c(0.9832, 
    0.8951, 0.8029, 0.6577, 0.592, 0.463, 0.2972, 0.2003, 0.1407, 
    0.1174, 0.9826, 0.8371, 0.6213, 0.5487, 0.4885, 0.3973, 0.2696, 
    0.181, 0.1409, 0.1056), LeafArea = c(5.9777, 5.9777, 5.9777, 
    5.9777, 5.9777, 5.9777, 5.9777, 5.9777, 5.9777, 5.9777, 6.218, 
    6.218, 6.218, 6.218, 6.218, 6.218, 6.218, 6.218, 6.218, 6.218
    ), nitorgen_rf = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0), droughtIndex = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), moisture_rf = c(1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
    degreeDay_rf = c(0.4405, 0.4405, 0.4405, 0.4405, 0.4405, 
    0.4405, 0.4405, 0.4405, 0.4405, 0.4405, 0.4405, 0.4405, 0.4405, 
    0.4405, 0.4405, 0.4405, 0.4405, 0.4405, 0.4405, 0.4405), 
    foliageWght = c(0.1471, 0.0473, 0.0389, 0.0301, 0.0255, 0.0197, 
    0.0149, 0.0085, 0.0068, 0.0055, 0.1511, 0.0584, 0.0452, 0.0315, 
    0.0198, 0.0165, 0.0123, 0.0084, 0.0068, 0.0047), twigWght = c(0.6236, 
    0.1251, 0.0929, 0.0627, 0.0483, 0.0455, 0.0674, 0.0488, 0.0376, 
    0.0286, 0.6462, 0.1708, 0.1169, 0.0672, 0.0448, 0.0639, 0.0655, 
    0.0482, 0.0376, 0.023), boleWght = c(5.6128, 1.1263, 0.8365, 
    0.5646, 0.4345, 0.2746, 0.134, 0.0291, 0.0148, 0.0077, 5.8161, 
    1.5374, 1.0519, 0.6045, 0.2776, 0.1766, 0.0823, 0.0281, 0.0149, 
    0.0045), deadFoliage = c(0.446, 0.446, 0.446, 0.446, 0.446, 
    0.446, 0.446, 0.446, 0.446, 0.446, 0.4278, 0.4278, 0.4278, 
    0.4278, 0.4278, 0.4278, 0.4278, 0.4278, 0.4278, 0.4278), 
    deadTwig = c(0.7874, 0.7874, 0.7874, 0.7874, 0.7874, 0.7874, 
    0.7874, 0.7874, 0.7874, 0.7874, 0.7322, 0.7322, 0.7322, 0.7322, 
    0.7322, 0.7322, 0.7322, 0.7322, 0.7322, 0.7322), deadbole = c(3.4762, 
    3.4762, 3.4762, 3.4762, 3.4762, 3.4762, 3.4762, 3.4762, 3.4762, 
    3.4762, 3.1449, 3.1449, 3.1449, 3.1449, 3.1449, 3.1449, 3.1449, 
    3.1449, 3.1449, 3.1449)), .Names = c("row", "col", "cell", 
"xcoord", "ycoord", "species", "age", "biomass", "stems", "slowGrowth", 
"DBH", "height", "availableLight", "light_rf", "LeafArea", "nitorgen_rf", 
"droughtIndex", "moisture_rf", "degreeDay_rf", "foliageWght", 
"twigWght", "boleWght", "deadFoliage", "deadTwig", "deadbole"
), row.names = c(NA, 20L), class = "data.frame")

这是我的代码。

for (i in 0:1) {
  t <- which(output100$cell == i)
  a <-max(output100[c(t),8])
  dom <- c(a, dom)
} 

我想获得&#34; t&#34;的最大单元格。当然,这只是一个小例子(0:1),我有更大的实际数据集。

1 个答案:

答案 0 :(得分:0)

不确定你的问题是什么,你的for循环为我运行。您是否忘了初始化dom

dom = NULL
for (i in 0:1) {
  t <- which(output100$cell == i)
  a <-max(output100[c(t),8])
  dom <- c(a, dom)
}
dom
## [1] 6.6135 6.3836

对我有用吗?你期待什么答案?

顺便说一句,这可能不是最有效的方法,因为你在for循环中增长结果向量。如果你有很多独特的单元格值,这将是缓慢的。您可以使用dplyr来实现类似功能,这也不需要您知道有多少cell个值:

library(dplyr)
output100 %>% 
  group_by(cell) %>% 
  summarise(max(biomass))
## # A tibble: 2 × 2
##    cell `max(biomass)`
##   <int>          <dbl>
## 1     0         6.3836
## 2     1         6.6135