Discussion:
[R] Storing vectors as vectors in a list without losing each individual vector
Patricia Seo
2014-10-13 23:27:35 UTC
Permalink
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!
Henrik Bengtsson
2014-10-14 09:25:14 UTC
Permalink
Post by Patricia Seo
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
Post by Patricia Seo
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.
Post by Patricia Seo
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.
Post by Patricia Seo
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
Post by Patricia Seo
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.

Does

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

do what you want?

Henrik
Post by Patricia Seo
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
Post by Patricia Seo
and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
David L Carlson
2014-10-14 13:15:28 UTC
Permalink
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!
Continue reading on narkive:
Loading...