[R] Vector of Vectors

jim holtman jholtman at gmail.com
Thu Apr 2 02:57:16 CEST 2009


This may help in understanding how to access the list: notice that I
am using numeric indices and using the '[[' operator

> # generate a list with a varying number of values
> myList <- list()  # initialize
> for (i in 1:10) myList[[i]] <- seq(i)
> myList
[[1]]
[1] 1

[[2]]
[1] 1 2

[[3]]
[1] 1 2 3

[[4]]
[1] 1 2 3 4

[[5]]
[1] 1 2 3 4 5

[[6]]
[1] 1 2 3 4 5 6

[[7]]
[1] 1 2 3 4 5 6 7

[[8]]
[1] 1 2 3 4 5 6 7 8

[[9]]
[1] 1 2 3 4 5 6 7 8 9

[[10]]
 [1]  1  2  3  4  5  6  7  8  9 10

> myList[[4]]  # 1,2,3,4
[1] 1 2 3 4
> myList[[4]][2:3]  # 2, 3
[1] 2 3
>


On Wed, Apr 1, 2009 at 12:58 PM, Shawn Garbett
<Shawn.P.Garbett at vanderbilt.edu> wrote:
> I have a matrix of data. I need to scan the matrix and find every sequence
> from maxima to maxima across a row. I can write a loop to do this easily.
> Problem is, I can't figure out how to store the results. Each result is a
> vector of widely varying lengths. Ideally I'd like a vector of these, i.e. a
> vector of vectors, so I can quickly iterate through them and compute
> correlation coefficients.
>
> Here's a transcript of my fuddling to date:
>
>> x <- c(1,2,3)
>> y <- c(4,5)
>> v <- list("1"=x, "2"=y)
>> unlist(v["1"])
> 11 12 13
>  1  2  3
>> unlist(v["1"])[1]
> 11
>  1
>> unlist(v["1"])[2]
> 12
>  2
>> unlist(v["1"])[3]
> 13
>  3
>> unlist(v["2"])[3]
> <NA>
>  NA
>> unlist(v["2"])[2]
> 22
>  5
>> v <- c(x,y)
>> v
> [1] 1 2 3 4 5
>> v <- vector()
>> v <- merge(v, x)
>> v
>     [,1] [,2]
> attr(,"row.names")
> integer(0)
>> v[1]
> [1] NA
>>
>
> As you can see, vectors aren't very cooperative and lists are downright
> baffling to me.
>
> Shawn Garbett <shawn.p.garbett at vanderbilt.edu>
> Vanderbilt Cancer Biology
> 220 Pierce Ave, PRB 715AA
> Nashville, TN 37232
> Office: 615.936.1975
> Cell: 615.397.8737
>
> ______________________________________________
> R-help at r-project.org 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list