Ken Termiso
2005-Apr-18 17:08 UTC
[R] Storing vectors as vectors and iterating through them
Hi all, I have a bunch of int vectors. Each vector holds a bunch of ints that correspond to row numbers of an existing matrix. I use the int vectors to pull out rows of data from a matrix, i.e. data <- my_matrix[int_vector,] I would like to store these int vectors in some sort of data structure that will preserve them as-is and allow iteration. I guess what I'm looking for would be something analogous to the java Vector class, as in this java-like pseudocode : Vector V = new Vector; V.add(a,b,c) // where a,b,c are lists for(int i = 0; i<V.size; i++) { List L = (List)Vector.get(i); plot(L); } The point is to iterate through the data structure containing the int vectors, and, for each int vector, do some clustering and plotting, but what I cannot find is a data structure in R that would support this.. trying c(a,b,c) does not preserve each int vector, but instead merges all the ints into one vector. I need to keep them separate (so I can create a separate plot for each vector). Thanks in advance, Ken
Wiener, Matthew
2005-Apr-18 17:15 UTC
[R] Storing vectors as vectors and iterating through them
Ken -- try using lists:> vec.list <- list(1:5, 2:4, 3:8) > vec.list <- c(vec.list, list(7:9)) > vec.list[[1]] [1] 1 2 3 4 5 [[2]] [1] 2 3 4 [[3]] [1] 3 4 5 6 7 8 [[4]] [1] 7 8 9 Then you can use "lapply" or "sapply", or just a "for" loop, to iterate over the list, applying your function to each element. Hope this helps, Matt Wiener -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ken Termiso Sent: Monday, April 18, 2005 1:09 PM To: r-help at stat.math.ethz.ch Subject: [R] Storing vectors as vectors and iterating through them Hi all, I have a bunch of int vectors. Each vector holds a bunch of ints that correspond to row numbers of an existing matrix. I use the int vectors to pull out rows of data from a matrix, i.e. data <- my_matrix[int_vector,] I would like to store these int vectors in some sort of data structure that will preserve them as-is and allow iteration. I guess what I'm looking for would be something analogous to the java Vector class, as in this java-like pseudocode : Vector V = new Vector; V.add(a,b,c) // where a,b,c are lists for(int i = 0; i<V.size; i++) { List L = (List)Vector.get(i); plot(L); } The point is to iterate through the data structure containing the int vectors, and, for each int vector, do some clustering and plotting, but what I cannot find is a data structure in R that would support this.. trying c(a,b,c) does not preserve each int vector, but instead merges all the ints into one vector. I need to keep them separate (so I can create a separate plot for each vector). Thanks in advance, Ken ______________________________________________ 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