[R] Storing vectors as vectors in a list without losing each individual vector

David L Carlson dcarlson at tamu.edu
Tue Oct 14 15:15:28 CEST 2014


If you just want to plot the various combinations of a set of variables/columns, you don't need a list, just another data frame/matrix with the combinations of the column numbers you want to plot:

> df <- matrix(rnorm(100), 10, 10)
> df <- data.frame(df)
> comb <- expand.grid(7:10, 7:10)
> comb <- comb[comb[,1] < comb[,2],]
> rownames(comb) <- NULL
> comb
  Var1 Var2
1    7    8
2    7    9
3    8    9
4    7   10
5    8   10
6    9   10
> windows(record=TRUE)
> apply(comb, 1, function(x) plot(df[,x[1]], df[,x[2]], 
+ main=paste("Plot of", x[1], "with", x[2])))
NULL

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Patricia Seo
Sent: Monday, October 13, 2014 6:28 PM
To: r-help at r-project.org
Subject: [R] Storing vectors as vectors in a list without losing each individual vector

Hi everyone,

My help request is similar to what was asked by Ken Termiso on April 18th, 2005. Link here: https://stat.ethz.ch/pipermail/r-help/2005-April/069729.html

Matt Wiener answered with suggesting a vector list where you hand type each of the vectors. This is not what I want to do. What I want to do is automate the process. So, in other words creating a list through a loop. 

For example:

My data frame is called "df" and I have four variables/vectors that are v7, v8, v9, 10. Each variable/vector is an integer (no character strings). I want to create a list called "Indexes" so that I can use this list for "for-in" loops to SEPARATELY plot each and every variable/vector. 

If I followed Matt Wiener's suggestion, I would input this:


Indexes = list()
Indexes[[1]] = df$v7 
Indexes[[2]] = df$v8
Indexes[[3]] = df$v9
Indexes[[4]] = df$v10

But if I want to include more than four variable/vectors (let's say I want to include 25 of them!), I do not want to have to type all of it. If I do the following command:

Indexes <- c(df$v7, df$v8, df$v9, df$v10)

then I run into the same problem as Ken Termiso with having all the integers in one vector. I need to keep the variables/vectors separate. 

Is this just not possible in R? Any help would be great. Thank you!

______________________________________________
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.



More information about the R-help mailing list