11thal11thal
11thal11thal

Reputation: 333

How to get a vector from an array in R

I'm sorry I don't know exactly how to word this question. Basically, I have an array of x and y coordinates called mydata that looks like this:

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

dput(mydata) gives:

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

str(mydata) gives:

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

I want to input this data into a function, f(x, y) , that takes two vectors (x and y). How can I input this data into the function? My instinct is to do f(mydata['x'], mydata['y'], but that's not working.

Upvotes: 1

Views: 317

Answers (1)

akrun
akrun

Reputation: 887118

If it is a 4D array, try

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

data

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

Upvotes: 1

Related Questions