每12个矩阵中的元素均值,重复一次序列12次,无需使用for循环。

huangapple go评论107阅读模式
英文:

Element-wise mean of every 12th matrix in array, repeated one sequence along 12 times, without for-loops

问题

我理解你只需要对代码部分进行翻译。以下是代码部分的翻译:

  1. # 我有一个维度为[360, 180, 396]的数组。这些是经度、纬度和年-月的数据,为33年的月度数据。元素是该经度/纬度下的百分比。
  2. # 我想从中制作一个摘要数组,以便在以后的分析中使用,而不必默认使用for循环。我想要获取每个月份在所有33年中的平均值,然后是所有年份的年平均值。
  3. # 这是我用来容纳数据的摘要数组的示例。
  4. mca <- array(data = NA,
  5. dim = c(360, 180, 13),
  6. dimnames = list(lon,
  7. lat,
  8. c(month.abb, "Ann")))
  9. # 这是此示例的较小测试输入和输出数组
  10. # 输入
  11. set.seed(42)
  12. smallin <- array(data = rnorm(n = 600, mean = 60, sd = 20),
  13. dim = c(5, 5, 24))
  14. # 输出(要填充)
  15. smallout <- array(data = NA,
  16. dim = c(5, 5, 13),
  17. dimnames = list(c("1", "2", "3", "4", "5"),
  18. c("-89.5", "-88.5", "-87.5", "-86.5", "-85.5"),
  19. c(month.abb, "Ann")))
  20. # 基于这个问题的第二个答案,我尝试了以下代码
  21. jan <- apply(ca, c(seq(from = 1, to = 385, by = 12)), mean)
  22. # 也可以这样写
  23. ind_jan <- c(seq(from = 1, to = 385, by = 12))
  24. jan <- apply(ca, ind_jan, mean)
  25. # 我认为这等同于
  26. jan <- apply(smallin, c(seq(from = 1, to = 13, by = 12)), mean)
  27. # 但是,我遇到了错误:
  28. # Error in apply(ca, c(seq(from = 1, to = 385, by = 12)), mean) :
  29. # 'MARGIN' does not match dim(X)
  30. # 我回到上面的查询并意识到margin = 1:2必须选择每个矩阵的全部内容(维度1和2)。
  31. # 所以使用这个方法,我可以得到所有矩阵的平均值,这应该是输出数组的百分比的年平均值[,,13]
  32. smallout[,,13] <- apply(smallin, 1:2, mean)
  33. # 但是我仍然不知道如何让它只平均每12个矩阵,从1开始,然后从2开始,然后从3开始...
  34. # 最接近的方法我认为是类似于以下的方式
  35. ind_jan <- c(seq(from = 1, to = 13, by = 12))
  36. smallout[,,1] <- apply(smallin[,,c(ind_jan)], 1:2, mean)
  37. # 为数组中的每个输出矩阵重复上述步骤。是否有更少手动/更有效/更好的方法?

希望这有助于你的理解。如果你有任何其他问题,请随时提出。

英文:

I have an array of dimensions [360, 180, 396]. These are longitude, latitude, and month-year for 33 years of monthly data. The elements are percentages for that lat/lon.

From this I want to make a summary array I will use in later analyses without defaulting to using for loops. I want to get the mean of each months data for all 33 years, then the annual average for all years.
This is the blank of the summary array I made to contain the data.

  1. mca &lt;- array(data = NA,
  2. dim = c(360,180,13),
  3. dimnames = list(lon,
  4. lat,
  5. c(month.abb, &quot;Ann&quot;)))

Here are smaller test input and output array for this example

  1. #input
  2. set.seed(42)
  3. smallin &lt;- array(data = rnorm(n = 600, mean = 60, sd = 20),
  4. dim = c(5, 5, 24))
  5. #output to fill
  6. smallout &lt;- array(data = NA,
  7. dim = c(5,5,13),
  8. dimnames = list(c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;),
  9. c(&quot;-89.5&quot;, &quot;-88.5&quot;, &quot;-87.5&quot;, &quot;-86.5&quot;, &quot;-85.5&quot;),
  10. c(month.abb, &quot;Ann&quot;)))

Based on the second answer to this question I tried

  1. jan &lt;- apply(ca, c(seq(from = 1, to = 385, by = 12)), mean)
  2. #also
  3. ind_jan &lt;- c(seq(from = 1, to = 385, by = 12))
  4. jan &lt;- apply(ca, ind_jan, mean)

which I think is equivalent to

  1. jan &lt;- apply(smallin, c(seq(from = 1, to = 13, by = 12)), mean)

thinking for margin I needed to put the 3rd dimension I wanted averaged, but received the error:

Error in apply(ca, c(seq(from = 1, to = 385, by = 12)), mean) :
'MARGIN' does not match dim(X)

I went back to the query above and realised margin = 1:2 must be selecting all of each matrix (dimensions 1 and 2). So using that I can get a mean of all the matrices which ought to be the annual average of the percentages for my output array [,,13],

  1. smallout[,,13] &lt;- apply(smallin, 1:2, mean)

but I still don't know how to get it to just average every 12th matrix starting from 1, then starting from 2, then starting from 3...

I have read the apply documentation, but found it unhelpful in this case/impenetrable. All of the suggested questions that came up seem to be in Python (or another language).

I also am not sure if I can do this all in one go, or a matrix by matrix passed to the output array by indexing as above.

The closest I can think is something like

  1. ind_jan &lt;- c(seq(from = 1, to = 13, by = 12))
  2. smallout[,,1] &lt;- apply(smallin[,,c(ind_jan)], 1:2, mean)

repeated for each of the output matrices in the array. Is there a less manual/more efficient/better way?

答案1

得分: 3

以下是您要翻译的内容:

考虑这个简化的数组 A(见下面的数据)。

  1. str(A)
  2. # int [1:2, 1:3, 1:6] 1 1 1 1 1 1 2 2 2 2 ...

我们可以使用 sapply 来"循环"遍历年份,并使用选项 simplify='array' 来获得年度平均值的数组,

  1. yrs <- seq_len(dim(A)[3]/nm)
  2. sapply(yrs, \(i) apply(A[, , 1:nm + i - 1], 1:2, mean), simplify='array')
  3. # , , 1
  4. #
  5. # [,1] [,2] [,3]
  6. # [1,] 2 2 2
  7. # [2,] 2 2 2
  8. #
  9. # , , 2
  10. #
  11. # [,1] [,2] [,3]
  12. # [1,] 2 2 2
  13. # [2,] 2 2 2

然后,相应地,获取跨年度的月度平均值:

  1. mnt <- seq_len(nm)
  2. sapply(mnt, \(i) apply(A[, , i], 1:2, mean), simplify='array')
  3. # , , 1
  4. #
  5. # [,1] [,2] [,3]
  6. # [1,] 1 1 1
  7. # [2,] 1 1 1
  8. #
  9. # , , 2
  10. #
  11. # [,1] [,2] [,3]
  12. # [1,] 2 2 2
  13. # [2,] 2 2 2
  14. #
  15. # , , 3
  16. #
  17. # [,1] [,2] [,3]
  18. # [1,] 3 3 3
  19. # [2,] 3 3 3

数据:

  1. nm <- 3 ## no. "months" ## actually 12 months in real years
  2. ny <- 2 ## no. "years" ## in your case 33
  3. A <- array(rep(1:nm, each=nm*ny), c(2, 3, nm*ny)) ## think this is your `ca`
英文:

Consider this simplified array A (see data below).

  1. str(A)
  2. # int [1:2, 1:3, 1:6] 1 1 1 1 1 1 2 2 2 2 ...

We can use sapply to "loop" over the years and option simplify=&#39;array&#39; to get back an array of the annual averages,

  1. yrs &lt;- seq_len(dim(A)[3]/nm)
  2. sapply(yrs, \(i) apply(A[, , 1:nm + i - 1], 1:2, mean), simplify=&#39;array&#39;)
  3. # , , 1
  4. #
  5. # [,1] [,2] [,3]
  6. # [1,] 2 2 2
  7. # [2,] 2 2 2
  8. #
  9. # , , 2
  10. #
  11. # [,1] [,2] [,3]
  12. # [1,] 2 2 2
  13. # [2,] 2 2 2

and, accordingly, of the monthly averages across the years:

  1. mnt &lt;- seq_len(nm)
  2. sapply(mnt, \(i) apply(A[, , i], 1:2, mean), simplify=&#39;array&#39;)
  3. # , , 1
  4. #
  5. # [,1] [,2] [,3]
  6. # [1,] 1 1 1
  7. # [2,] 1 1 1
  8. #
  9. # , , 2
  10. #
  11. # [,1] [,2] [,3]
  12. # [1,] 2 2 2
  13. # [2,] 2 2 2
  14. #
  15. # , , 3
  16. #
  17. # [,1] [,2] [,3]
  18. # [1,] 3 3 3
  19. # [2,] 3 3 3

Data:

  1. nm &lt;- 3 ## no. &quot;months&quot; ## actually 12 months in real years
  2. ny &lt;- 2 ## no. &quot;years&quot; ## in your case 33
  3. A &lt;- array(rep(1:nm, each=nm*ny), c(2, 3, nm*ny)) ## think this is your `ca`

答案2

得分: 2

你可以通过将最后一维分割成包含月份和年份的单独维度来为数组添加另一个维度。

使用以下代码:

  1. i <- dim(smallin)
  2. dim(smallin) <- c(i[1:2], 12L, i[3]/12L)

使用这个代码,你可以获得每个月份在所有年份中的平均值:

  1. apply(smallin, 1:3, mean)

每个月的平均值将在结果中显示。

年度平均值(单年):

  1. apply(smallin, c(1,2,4), mean)

年度平均值(所有年份):

  1. apply(smallin, 1:2, mean)

这将为你提供每年的年度平均值和所有年份的年度平均值。

英文:

You can add another dimension to the array by splitting the last dimension, containing month and year to separate dimensions for month and year.

  1. i &lt;- dim(smallin)
  2. dim(smallin) &lt;- c(i[1:2], 12L, i[3]/12L)

With this you can get the averages for each month over all years with:

  1. apply(smallin, 1:3, mean)
  2. #, , 1
  3. #
  4. # [,1] [,2] [,3] [,4] [,5]
  5. #[1,] 73.66338 58.35988 72.33907 62.19628 52.08766
  6. #[2,] 61.95544 79.93891 75.27725 49.30859 44.07820
  7. #[3,] 64.02119 68.98285 35.76780 35.06961 58.79089
  8. #[4,] 73.67935 67.72028 50.90479 23.22819 72.14434
  9. #[5,] 62.57796 59.03798 64.53486 83.65987 97.04576
  10. #
  11. #...
  12. #
  13. #, , 12
  14. #
  15. # [,1] [,2] [,3] [,4] [,5]
  16. #[1,] 83.55254 68.77645 48.88358 52.99573 56.82992
  17. #[2,] 83.47723 39.02472 95.08051 65.97988 54.00097
  18. #[3,] 47.59936 36.93396 38.35189 57.86126 83.99976
  19. #[4,] 73.00906 53.71818 36.93229 80.85843 39.27094
  20. #[5,] 81.67441 64.50031 62.71359 56.27758 54.01388

The annual average for single years:

  1. apply(smallin, c(1,2,4), mean)
  2. #, , 1
  3. #
  4. # [,1] [,2] [,3] [,4] [,5]
  5. #[1,] 60.77253 60.15417 54.71206 67.31820 62.05012
  6. #[2,] 56.60298 59.14604 73.17469 57.66912 53.36540
  7. #[3,] 56.52924 56.31096 58.73874 67.47850 59.06819
  8. #[4,] 67.75999 56.45636 49.43743 55.14660 65.46497
  9. #[5,] 60.28056 62.17656 55.08681 54.15788 60.05240
  10. #
  11. #, , 2
  12. #
  13. # [,1] [,2] [,3] [,4] [,5]
  14. #[1,] 60.55035 65.21223 59.92112 59.75500 69.77088
  15. #[2,] 60.89782 54.59722 55.17699 59.06815 60.03906
  16. #[3,] 58.85733 54.02893 47.31326 63.10434 59.56569
  17. #[4,] 60.96362 61.82648 55.45109 54.50272 45.21176
  18. #[5,] 59.94452 54.31497 60.64839 64.65777 80.86525

The annual average over all years:

  1. apply(smallin, 1:2, mean)
  2. # [,1] [,2] [,3] [,4] [,5]
  3. #[1,] 60.66144 62.68320 57.31659 63.53660 65.91050
  4. #[2,] 58.75040 56.87163 64.17584 58.36864 56.70223
  5. #[3,] 57.69329 55.16994 53.02600 65.29142 59.31694
  6. #[4,] 64.36180 59.14142 52.44426 54.82466 55.33836
  7. #[5,] 60.11254 58.24577 57.86760 59.40782 70.45883

答案3

得分: 1

我确信有更好的方法,如果有人知道,请告诉我,我还是很愿意学习的,但是一旦我弄清楚如何做索引以便只选择每个月的数据来求均值,下面的方法就能够工作。

  1. mca[,,1] <- apply(ca[,,c(seq(from = 1, to = 396, by = 12))], 1:2, mean)
  2. mca[,,2] <- apply(ca[,,c(seq(from = 2, to = 396, by = 12))], 1:2, mean)
  3. mca[,,3] <- apply(ca[,,c(seq(from = 3, to = 396, by = 12))], 1:2, mean)
  4. mca[,,4] <- apply(ca[,,c(seq(from = 4, to = 396, by = 12))], 1:2, mean)
  5. mca[,,5] <- apply(ca[,,c(seq(from = 5, to = 396, by = 12))], 1:2, mean)
  6. mca[,,6] <- apply(ca[,,c(seq(from = 6, to = 396, by = 12))], 1:2, mean)
  7. mca[,,7] <- apply(ca[,,c(seq(from = 7, to = 396, by = 12))], 1:2, mean)
  8. mca[,,8] <- apply(ca[,,c(seq(from = 8, to = 396, by = 12))], 1:2, mean)
  9. mca[,,9] <- apply(ca[,,c(seq(from = 9, to = 396, by = 12))], 1:2, mean)
  10. mca[,,10] <- apply(ca[,,c(seq(from = 10, to = 396, by = 12))], 1:2, mean)
  11. mca[,,11] <- apply(ca[,,c(seq(from = 11, to = 396, by = 12))], 1:2, mean)
  12. mca[,,12] <- apply(ca[,,c(seq(from = 12, to = 396, by = 12))], 1:2, mean)
  13. mca[,,13] <- apply(ca, 1:2, mean)
英文:

I am sure there is a better way, and if anyone has it I am still keen to learn, but the below turns out to work, once I figure out how to do the indexing in order to select just each months data to take the mean using apply.

  1. mca[,,1] &lt;- apply(ca[,,c(seq(from = 1, to = 396, by = 12))], 1:2, mean)
  2. mca[,,2] &lt;- apply(ca[,,c(seq(from = 2, to = 396, by = 12))], 1:2, mean)
  3. mca[,,3] &lt;- apply(ca[,,c(seq(from = 3, to = 396, by = 12))], 1:2, mean)
  4. mca[,,4] &lt;- apply(ca[,,c(seq(from = 4, to = 396, by = 12))], 1:2, mean)
  5. mca[,,5] &lt;- apply(ca[,,c(seq(from = 5, to = 396, by = 12))], 1:2, mean)
  6. mca[,,6] &lt;- apply(ca[,,c(seq(from = 6, to = 396, by = 12))], 1:2, mean)
  7. mca[,,7] &lt;- apply(ca[,,c(seq(from = 7, to = 396, by = 12))], 1:2, mean)
  8. mca[,,8] &lt;- apply(ca[,,c(seq(from = 8, to = 396, by = 12))], 1:2, mean)
  9. mca[,,9] &lt;- apply(ca[,,c(seq(from = 9, to = 396, by = 12))], 1:2, mean)
  10. mca[,,10] &lt;- apply(ca[,,c(seq(from = 10, to = 396, by = 12))], 1:2, mean)
  11. mca[,,11] &lt;- apply(ca[,,c(seq(from = 11, to = 396, by = 12))], 1:2, mean)
  12. mca[,,12] &lt;- apply(ca[,,c(seq(from = 12, to = 396, by = 12))], 1:2, mean)
  13. mca[,,13] &lt;- apply(ca, 1:2, mean)

huangapple
  • 本文由 发表于 2023年4月11日 03:11:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定