复杂数组的平均值

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

Average for a complex array

问题

针对下面的对象(polysst),我该如何计算海表温度数组每个“切片”的平均值?

> str(polysst)
List of 6
 $ sea_surface_temperature: num [1:73, 1:25, 1:384] NA NA NA NA NA NA NA NA NA NA ...
 $ datasetname            : chr "erdPH53sstdmday"
 $ longitude              : num [1:73(1d)] -127 -127 -127 -127 -127 ...
 $ latitude               : num [1:25(1d)] 46 46 46.1 46.1 46.1 ...
 $ altitude               : logi NA
 $ time                   : POSIXlt[1:384], format: "1990-01-17" "1990-02-15" "1990-03-17" "1990-04-16" ...
 - attr(*, "class")= chr [1:2] "list" "rxtracto3D"

> str(polysst$sea_surface_temperature)
 num [1:73, 1:25, 1:384]

> class(polysst$sea_surface_temperature)
[1] "array"

polysst$sea_surface_temperature 是一个 3x3 的数组。它表示一个空间网格,包括 73 * 25 个网格单元,每个单元代表 384 个日期(polysst$time)的海表温度。每个值是在该日期上该网格单元的海表温度。我想要计算每个 73 * 25 的数组/矩阵的平均值,针对每个日期的情况。实际上,这将返回该日期上整个空间网格的平均温度。我可以使用以下代码来计算单个“切片”的平均值:

test8 <- mean(as.numeric(polysst$sea_surface_temperature[,,1]), na.rm = TRUE)

如何为整个数组执行这个操作?最好能保留日期,以确保列表在这个过程中没有混乱。因此,我想要一个包含日期和平均值的数据框。

对于这种复杂的数组/对象,我还是比较新手,希望我没有漏掉像 mean(polysst$sea_surface_temperature, by = date) 这样的明显方法...

谢谢 复杂数组的平均值

英文:

For the object below (polysst), how can I take the average of each "slice" of the sea_surface_temperature array?

&gt; str(polysst)
List of 6
 $ sea_surface_temperature: num [1:73, 1:25, 1:384] NA NA NA NA NA NA NA NA NA NA ...
 $ datasetname            : chr &quot;erdPH53sstdmday&quot;
 $ longitude              : num [1:73(1d)] -127 -127 -127 -127 -127 ...
 $ latitude               : num [1:25(1d)] 46 46 46.1 46.1 46.1 ...
 $ altitude               : logi NA
 $ time                   : POSIXlt[1:384], format: &quot;1990-01-17&quot; &quot;1990-02-15&quot; &quot;1990-03-17&quot; &quot;1990-04-16&quot; ...
 - attr(*, &quot;class&quot;)= chr [1:2] &quot;list&quot; &quot;rxtracto3D&quot;

&gt; str(polysst$sea_surface_temperature)
 num [1:73, 1:25, 1:384]

&gt; class(polysst$sea_surface_temperature)
[1] &quot;array&quot;

polysst$sea_surface_temperature is a 3*3 array. This represents a spatial grid that is 73 * 25 grid cells, for each of 384 dates (polysst$time). Every value is the sea surface temperature of that grid cell on that date. I am trying to take the average of each 73 * 25 array/matrix, for each of the 384 dates. In effect, this will return the average temperature across the entire spatial grid on that date. I can do this for a single "slice" with this code:

test8 &lt;- mean(as.numeric(polysst$sea_surface_temperature[,,1]), na.rm = TRUE)

How can I do this for the whole array? It would be good if we could preserve the date as well, just to make sure that the list didn't get scrambled in the process. So, a data frame with date and average value is what I'm after.

I'm new to this sort of complex array / object, so I'm hoping that I've missed something obvious like mean(polysst$sea_surface_temperature, by = date)...

Thanks 复杂数组的平均值

答案1

得分: 0

好的,我终于解决了,将其发布在这里,以防其他人遇到相同的问题。

test63 <- apply(polysst$sea_surface_temperature, 3, mean, na.rm = TRUE)

这不会保留日期,但快速检查确认它是按照时间顺序的,所以我只需将其添加到一个带有按顺序排列的日期的数据框中。

英文:

Alright, I've finally solved, posting here incase anyone else is having the same problem.

test63 &lt;- apply(polysst$sea_surface_temperature, 3, mean, na.rm = TRUE)

This doesn't preserve date, but a quick spot check confirmed that it is in chronological order, so I can just add it to a data frame with the dates in order.

huangapple
  • 本文由 发表于 2023年5月24日 17:35:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322095.html
匿名

发表评论

匿名网友

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

确定