1

I'm trying to create a data structure to store vectors. What I'm looking for is a 3x2 matrix with vectors of different lenght inside.

             front        side 
Original  c(dim=221) c(dim =200)
zscore    c(dim=221) c(dim =200)
smoothed  c(dim=221) c(dim =200)

I have tried doing:

dataset <- array(dim=c(3,2))
rownames(dataset) <- c("original","zscores","smoothed")
colnames(dataset) <- c("front", "side")

dataset["original", "side"] <- myNumericVectorOfLength221
dataset["original", "front"] <- myNumericVectorOfLength200

But it throws an error of "not the same size". A three dimensional array like: dataset <- array(dim=c(3,2,221)) didn't work because of the differences of length and if I create a matrix of vectors like (dataset["original", "side"] <- list(c(1,2,3))) I lose the col/rownames.

Is there any solution that fits my idea? Thanks in advance.

Pop
  • 12,135
  • 5
  • 55
  • 68
Sergiodiaz53
  • 1,268
  • 2
  • 14
  • 23
  • 1
    Maybe [this will help](http://stackoverflow.com/questions/3699405/how-to-cbind-or-rbind-different-lengths-vectors-without-repeating-the-elements-o) – Sotos Jan 30 '17 at 10:17
  • 1
    You can assign names to elements of alist, wouldn't that suffice? – LAP Jan 30 '17 at 10:19
  • Can you explain it a little bit more, please? @LeoP. – Sergiodiaz53 Jan 30 '17 at 11:20
  • You could try a three dimensional array with the third dimension as large as your biggest vector, and fill the other vectors with NAs, so they all have the same length. – Rodrigo Jan 30 '17 at 12:07

2 Answers2

2

You can create a multi-dimensional list, e.g. :

# prepare some random vectors with different lengths
myNumericVectorOfLength221 <- rnorm(221)
myNumericVectorOfLength200 <- rnorm(220)

# create a multi dimensional list
dataset <- array(list(),dim=c(3,2))
rownames(dataset) <- c("original","zscores","smoothed")
colnames(dataset) <- c("front", "side")

# fill some cells
dataset[["original", "side"]] <- vec10
dataset[["zscores","front"]] <- vec3

# let's see the whole matrix
> dataset
         front     side      
original NULL      Integer,10
zscores  Integer,3 NULL      
smoothed NULL      NULL

# let's get one of the added vector
> dataset[["original", "side"]]
[1]  1  2  3  4  5  6  7  8  9 10
digEmAll
  • 56,430
  • 9
  • 115
  • 140
1

You could save your vectors in a list and name the elements of said list.

Either create one list with all vectors

mylist <- list("front_Original" = vector1, "front_zscore" = vector2, ...)

or create sublists for your factors

mylist2 <- list("front" = list("Original" = vector1, "zscore" = vector2, ...), 
               "side" = list("Original" = vector3, "zscore" = vector4))

List elements can be of different classes and dimensions, therefore you would not run into any problems this way. Refering to a list works similarly to a data.frame, so you could call a certain vector of mylist2 with mylist2$front$Original or with mylist2[[1]][[1]].

LAP
  • 6,605
  • 2
  • 15
  • 28
  • Ok, I understand you now. The main problem is that I'm using a variable "type" to get the a string like "Original" so I would like to call the array like: dataset[type, "side"]. I have just tried to do the same with 'mylist2$front$type and it doesn't work. Is there anyway to call the list this way? thanks for your help. – Sergiodiaz53 Jan 30 '17 at 11:55