mauede at alice.it
2009-Jan-05 11:30 UTC
[R] How can pairs of values be stored in a 2D matrix ?
My question is motivated by dealing with pairs of values, like (3,12.5), (16,2.98), and so on that are mapped to a cartesian plain (Time, Frequence) I miss C multidimensional arrays. I am trying to simulate the 3rd dimension by declaring a matrix of lists. R seems to accept the definition of such a matrix. But I'm experiencing some difficulty at storing pairs of data in it. In the following are some of my attempts. I would greatly appreciate some help.> A <- matrix(data ="list", nrow=2, ncol=4) > A[,1] [,2] [,3] [,4] [1,] "list" "list" "list" "list" [2,] "list" "list" "list" "list"> A[[1]] <- list(data="12.3",variable="d1")Error in A[[1]] <- list(data = "12.3", variable = "d1") : more elements supplied than there are to replace> A[1,2] <- list(data="12.3",variable="d1")Error in A[1, 2] <- list(data = "12.3", variable = "d1") : number of items to replace is not a multiple of replacement length> A[[1,2]] <- list(data="12.3",variable="d1")Error in A[[1, 2]] <- list(data = "12.3", variable = "d1") : more elements supplied than there are to replace> A[[1,1]] <- list(data="12.3",variable="d1")Error in A[[1, 1]] <- list(data = "12.3", variable = "d1") : more elements supplied than there are to replace> A[[1]][1] <- 10 > A[[1]][2] <- "f1-t1"Error in A[[1]][2] <- "f1-t1" : more elements supplied than there are to replace> A[[1]][2] <- "d1"Error in A[[1]][2] <- "d1" : more elements supplied than there are to replace Regards, Maura tutti i telefonini TIM! [[alternative HTML version deleted]]
Stavros Macrakis
2009-Jan-05 17:02 UTC
[R] How can pairs of values be stored in a 2D matrix ?
On Mon, Jan 5, 2009 at 6:30 AM, <mauede at alice.it> wrote:> My question is motivated by dealing with pairs of values, like (3,12.5), (16,2.98), and so on > that are mapped to a cartesian plain (Time, Frequence) > I miss C multidimensional arrays. I am trying to simulate the 3rd dimension by declaring a matrix of lists.I think the standard R way to do this would be with a 3-dimensional array. Many functions are designed to work well with n-dimensional arrays, including apply and the plyr package. But R does allow you to make a matrix of lists.>> A <- matrix(data ="list", nrow=2, ncol=4)This creates a matrix of strings (character vector). What you want is a matrix of lists, something like this: A<-matrix(data=list(), nrow=2,ncol=4) Then I think all your examples below work. -s PS Note that it does *not* work to write data=NULL, because NULL is not of class/mode 'list', even though the resulting matrix is filled with NULL.
Reasonably Related Threads
- R version 3.3.2, Windows 10: Applying a function to each possible pair of rows from two different data-frames
- x,y vs row,column
- how to estimate multidimensional spectral measure of coherence
- how to recover a list structure
- possible bug in merge with duplicate blank names in 'by' field.