R - 组合 3D 矩阵和单个向量,然后选择特定月份的数据

发布于 2025-01-02 10:18:39 字数 453 浏览 3 评论 0原文

我现在有一个日期向量A(362行),我有一个3D矩阵B(尺寸360*180*3620

> str(ssta_date)
 POSIXlt[1:362], format: "1981-11-15" "1981-12-15" "1982-01-15" "1982-02-15" "1982-03-15" ...

> dim(ssta_sst)
[1] 360 180 362

是可以将 AB 组合为数据框或其他内容。它必须与 B [,,362] 中的每个日期和每个温度相匹配。所以每个日期都会对应一个360*180的矩阵。

因为我计划执行此操作来选择某些月份数据,所以是否有任何快捷方式可以在不合并两个数据集的情况下进行月份数据选择?

i am now having a single date vector A (362 rows) and i have a 3D matrix B (dimensions 360*180*3620)

> str(ssta_date)
 POSIXlt[1:362], format: "1981-11-15" "1981-12-15" "1982-01-15" "1982-02-15" "1982-03-15" ...

> dim(ssta_sst)
[1] 360 180 362

is it possible to combine A and B as a data frame or something else. it must match each date and each temperature in the B [,,362]. so each date will corresponding to a 360*180 matrix.

Because i am planning to do this t to select certain month data, is there any shortcut to do month data selection without combine the two data sets?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2025-01-09 10:18:39

如果我明白你想要什么
您可以使用 melt 将数组转换为 data.frame,
然后使用merge将其与其他数据连接起来。
如果你想在末尾有一个数组,你可以使用acast
将 data.frame 转换回数组。

# Sample data
n <- 10
d <- array( 
  rnorm(n^3),
  dim = c(n,n,n), 
  dimnames = list(
    One   = LETTERS[1:n], 
    Two   = LETTERS[1:n], 
    Three = LETTERS[1:n]
  ) 
)
x <- data.frame( One=LETTERS[1:n], x=runif(n) )

library(reshape2)
d <- melt(d)
d <- merge( d, x, by="One", all=TRUE )
d$result <- d$value + d$x # Do something with the data
acast( d, One ~ Two ~ Three, value.var="result" )

If I understand what you want,
you can use melt to convert your array to a data.frame,
and then use merge to join it with the other piece of data.
If you want an array at the end, you can use acast
to convert the data.frame back to an array.

# Sample data
n <- 10
d <- array( 
  rnorm(n^3),
  dim = c(n,n,n), 
  dimnames = list(
    One   = LETTERS[1:n], 
    Two   = LETTERS[1:n], 
    Three = LETTERS[1:n]
  ) 
)
x <- data.frame( One=LETTERS[1:n], x=runif(n) )

library(reshape2)
d <- melt(d)
d <- merge( d, x, by="One", all=TRUE )
d$result <- d$value + d$x # Do something with the data
acast( d, One ~ Two ~ Three, value.var="result" )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文