【问题标题】:How to get a vector from an array in R如何从R中的数组中获取向量
【发布时间】:2021-05-13 09:14:27
【问题描述】:

对不起,我不知道如何准确地表达这个问题。基本上,我有一个名为 mydata 的 x 和 y 坐标数组,如下所示:

              x        y
  [1,]       12        1
  [2,]       13        2
  [3,]      NaN      NaN
  [4,]      NaN      NaN
  [5,]       64        5

dput(mydata) 给出:

structure(c(12, 13, 
NaN, NaN, 64 ... .Dim = c(934L, 
2L, 2L, 3L), .Dimnames = list(NULL, c("x", "y"), NULL, NULL))

str(mydata) 给出:

num [1:934, 1:2, 1:2, 1:3] 558 NaN NaN NaN NaN ...
 - attr(*, "dimnames")=List of 4
  ..$ : NULL
  ..$ : chr [1:2] "x" "y"
  ..$ : NULL
  ..$ : NULL

我想将此数据输入到函数f(x, y) 中,该函数采用两个向量(x 和y)。如何将这些数据输入到函数中?我的直觉是做 f(mydata['x'], mydata['y'],但这不起作用。

【问题讨论】:

  • 你的结构给了我错误。你能显示str(mydata)
  • 您是否有类似mydata <- array(1:40, dim = c(5, 2, 2, 2), dimnames = list(NULL, c("x", "y"), NULL, NULL)) 的数据并想提取所有“x”、“y”列?
  • 是的,我想从这个数组中提取 x 和 y 列

标签: r arrays vector


【解决方案1】:

如果是4D数组,试试

f(c(mydata[, "x", ,]),  c(mydata[, "y", ,]))

数据

mydata <- array(1:40, dim = c(5, 2, 2, 2), 
   dimnames = list(NULL, c("x", "y"), NULL, NULL))

【讨论】:

  • 当我尝试查看 mydata[, "x"] 我收到以下错误消息:Error in mydata[, "x"] : incorrect number of dimensions
  • @11thal11thal 这是基于您展示的示例。好像是matrix。你能用dput(mydata)str(mydata)更新你的帖子吗
  • 完成——当我检查数据的类时,它显示为数组
  • @11thal11thal 请显示str(mydata),因为您的结构不完整并给出错误
  • 非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-13
  • 1970-01-01
  • 2015-05-16
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
相关资源
最近更新 更多