[R] question of one matrix column being treated as list rather than vector

Tamas K Papp tpapp at Princeton.EDU
Sat Nov 4 00:27:18 CET 2006


On Fri, Nov 03, 2006 at 02:47:37PM -0800, Waverley wrote:

Hi,

You probably created that matrix from a list.  Eg

> a <- matrix(list(1,2,3,4),2,2)
> a
     [,1] [,2]
[1,] 1    3   
[2,] 2    4   
> a[,2]
[[1]]
[1] 3

[[2]]
[1] 4

> b <- matrix(1:4,2,2)
> b[,2]
[1] 3 4

Both a and b are matrices, but note:

> is.recursive(a)
[1] TRUE
> is.recursive(b)
[1] FALSE

Possible solutions: generate the matrix like b above, or use as.vector
before calling max.  Read ?[ and ?[[.

HTH,

Tamas


> Browse[1]> each.uaa.data
>       [,1] [,2]
>  [1,] 33   152.3530
>  [2,] 33   264.7059
>  [3,] 51   583.3333
>  [4,] 33   323.3333
>  [5,] 50   292.549
>  [6,] 33   300
>  [7,] 56   104.1176
>  [8,] 52   246.6667
>  [9,] 53   242.5490
> [10,] 33   212.9412
> 
> when I tried to get each.uaa.data[,2], it gave me a list, when I expect a
> vector so that I can find the max(each.uaa.data[,2]), this max() failed.
> I have checked is.matrix (each.uaa.data) and it is TRUE.  I have a created a
> = matrix (12, 12, 2), a[,2] give me a vector of 12 of 12s which is what I
> have expected.  Can someone help as how to prevent it becomes a list?  I am
> new and thanks much for the help.
> 
> Browse[1]> (each.uaa.data[,2])
> [[1]]
> [1] 152.3530
> 
> [[2]]
> [1] 264.7059
> 
> [[3]]
> [1] 583.3333
> 
> [[4]]
> [1] 323.3333
> 
> [[5]]
> [1] 292.549
> 
> [[6]]
> [1] 300
> 
> [[7]]
> [1] 104.1176
> 
> [[8]]
> [1] 246.6667
> 
> [[9]]
> [1] 242.5490
> 
> [[10]]
> [1] 212.9412
> 
> 
> -- 
> Waverley @ Palo Alto
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list