[R] vector entry in matix

David Winsemius dwinsemius at comcast.net
Thu Jul 5 19:05:26 CEST 2012


On Jul 5, 2012, at 11:22 AM, R. Michael Weylandt wrote:

> It is possible to put dimensionality on a list (i.e., a generic
> vector), which might be what you're looking for.
>
> x <- list(1:4, letters[1:4], function(x,y) x + y, rnorm(50))
> dim(x) <- c(2,2)
>
> x[[1,2]]
>
> x[[2,2]]
>
> x[[3,2]] # Error

That constructs a 2 x 2 matrix whose elements are lists:

 > x
      [,1]        [,2]
[1,] Integer,4   ?
[2,] Character,4 Numeric,50
 > is.matrix(x)
[1] TRUE

I suppose one could take that approach to constructing an Excel-mimic,  
since the functional element survives that passage:

 > x[1,2][[1]](3,4)
[1] 7


>
> Best,
> Michael
>
> On Thu, Jul 5, 2012 at 8:19 AM, Thomas C. <thomas.csapak at gmail.com>  
> wrote:
>> hi,
>>
>> i'm trying to figure out if there's any possibility to write a  
>> whole vector
>> into a matrix or data.frame or something like that. i don't mean
>> transormation. Here an example:
>>
>>       [,1]  [,2]
>> [1,] "a" "d"
>> [2,] "b" "e"
>> [3,] "c" "f"
>>
>>
>> where e.g.  a is a<-c(0,1) vector of length 2, b a vector of length  
>> 4,... (i
>> know that the matrix enties are strings :)). i'm trying to put some
>> variables into a matrix or data.frame, but i couldn't handle it. is
>> something like that possible in R?

The matrix classes objects loose their names, however. You will not  
have a,b, ... as handles in that matrix. You could create row and  
column names.

>>
>> thanks for your help!
>>


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list