0

How can I create a 3D array with multiple data frames (or, I can convert them to arrays if that makes more sense) as the third dimension? So, I have multiple data frames that have rows and columns (are 2D), and I want them in one 3D array. The columns are the same across all data frames, but the number of rows is variable.

a <- c(1, 2)
b <- c(2, 1)
df <- data.frame(a, b)

c <- c(3, 4)
d <- c(4, 3)
df2 <- data.frame(c, d)

I need df and df2 to be the third dimension in an array

Lisa
  • 909
  • 2
  • 13
  • 31
  • 1
    Do you just mean a `list`? `list(df1, df2, df3)` ? If so, that was the exact output I suggested in my answer to your previous question. – thelatemail Nov 22 '18 at 00:34
  • 1
    Something like this - https://stackoverflow.com/a/36440267/496803 - in particular `simplify2array(list(x1, x2, x3))` where `x1/2/3` are matrices all of the same size. – thelatemail Nov 22 '18 at 00:53
  • Actually, I'm trying to do something different now. I want to use dynamic time warping on a multivariate time series. According to this website: dtw.r-forge.r-project.org, my data should be arranged in this format "x[time,component,series]". If I use `list`, it creates a list of data frames, instead of an object that is accessible in three dimensions like `[1, 2, 1]` – Lisa Nov 22 '18 at 00:53
  • Maybe your question becomes more clear with some example code. – Nairolf Nov 22 '18 at 00:54
  • 1
    https://stackoverflow.com/questions/4310727/what-is-rs-multidimensional-equivalent-of-rbind-and-cbind – Chris Nov 22 '18 at 00:56
  • thanks @Chris - just what I was looking for. I'm getting an error message that `arg 'X2' has dims=917, 30, 1; but need dims=839, 30, X` - I'm not sure why the third dimension of my data frame is `X`? Or maybe I am misinterpreting this error. Reading the manual for `abind`, it says that you have the data differ across 1 dimension. – Lisa Nov 22 '18 at 01:02
  • 1
    As per the previous answer at the linked question - `simplify2array(lapply(list(df,df2),as.matrix))` works. – thelatemail Nov 22 '18 at 01:04
  • @thelatemail, maybe I am doing something wrong, but this code only gives me a 1 dimensional list of the data frames. For example, when I put in `test <-simplify2array(lapply(list(df,df2),as.matrix))` and then `test[1, 1, 1]` R returns the error `incorrect number of dimensions`, but returns data if I use `test[1]` – Lisa Nov 22 '18 at 01:08
  • 1
    Using exactly what you have above in the question and your code to create and subset `test`, I get `1` - the first column of the first row in the first strata. Maybe try with a fresh R session. – thelatemail Nov 22 '18 at 01:10
  • Perhaps there is something wrong with the way I created the data frame (and perhaps that's also why I couldn't get your other answer to work). I have tried restarting the session and checking my data with `is.data.frame` and `is.list`. Hopefully I can get it figured out. Thanks for your help. – Lisa Nov 22 '18 at 01:36
  • 1
    @Lisa - try an `df <- as.data.frame(df)` on each object first. They might have some wacky other attributes that are not a pure `data.frame` causing grief. – thelatemail Nov 22 '18 at 01:50
  • I have figured out the problem: I have data sets that are 23 x variable row length x 1. I thought that since only the number of rows is different, that would be fine, but it appears to be causing the problem. I think the solution is to pad my data with NAs. Thanks! – Lisa Nov 23 '18 at 13:23

0 Answers0