[R] Adding a new variable to each element of a list
arun
smartpink111 at yahoo.com
Sun Nov 25 00:22:13 CET 2012
HI,
The example data you gave have only one row for the 1st element of list. So, it would be better to add it as:
lapply(result,function(x) {x[[2]]<-y
return(x)})
#$`Error: subject`
#Component 1 :
# Df Sum Sq Mean Sq F value Pr(>F)
#Residuals 4 12.4 3.1
#
#Component 2 :
#[1] 0.5000000 0.7724138
-----------------------------
You can also do:
lapply(result,function(x) {x[[1]][6]<-y[1]
x[[1]][7]<-y[2]
return(x)})
#$`Error: subject`
# Df Sum Sq Mean Sq F value Pr(>F) V6 V7
#Residuals 4 12.4 3.1 0.5 0.7724
#$`Error: subject:myfactor`
# Df Sum Sq Mean Sq F value Pr(>F) V6 V7
#myfactor 2 14.93 7.467 13.58 0.002683 0.5 0.7724
#Residuals 8 4.40 0.550 0.5 0.7724 #Here it got repeated
May be there are better methods
A.K.
----- Original Message -----
From: Stephen Politzer-Ahles <politzerahless at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Saturday, November 24, 2012 5:33 PM
Subject: [R] Adding a new variable to each element of a list
Hello,
I have a list of data with multiple elements, and each element in the list
has multiple variables in it. Here's an example:
### Make the fake data
dv <- c(1,3,4,2,2,3,2,5,6,3,4,4,3,5,6)
subject <- factor(c("s1","s1","s1","s2","s2","s2","s3","s3","s3",
"s4","s4","s4","s5","s5","s5"))
myfactor <- factor(c("f1","f2","f3","f1","f2","f3","f1","f2","f3",
"f1","f2","f3","f1","f2","f3"))
mydata <- data.frame(dv, subject, myfactor)
### Do the anova and store the summary in "result"
mydata.aov <- aov( dv ~ myfactor + Error(subject/myfactor), mydata )
( result <- summary( mydata.aov ) ) # see the anova
str(result)
List of 2
$ Error: subject :List of 1
..$ :Classes ‘anova’ and 'data.frame': 1 obs. of 5 variables:
.. ..$ Df : num 4
.. ..$ Sum Sq : num 12.4
.. ..$ Mean Sq: num 3.1
.. ..$ F value: num NA
.. ..$ Pr(>F) : num NA
..- attr(*, "class")= chr [1:2] "summary.aov" "listof"
$ Error: subject:myfactor:List of 1
..$ :Classes ‘anova’ and 'data.frame': 2 obs. of 5 variables:
.. ..$ Df : num [1:2] 2 8
.. ..$ Sum Sq : num [1:2] 14.9 4.4
.. ..$ Mean Sq: num [1:2] 7.47 0.55
.. ..$ F value: num [1:2] 13.6 NA
.. ..$ Pr(>F) : num [1:2] 0.00268 NA
..- attr(*, "class")= chr [1:2] "summary.aov" "listof"
As you can see, each element in "result" has several variables (Df, Sum Sq,
Mean Sq, F value, Pr(>F)):
str( result[[2]][[1]] )
Classes ‘anova’ and 'data.frame': 2 obs. of 5 variables:
$ Df : num 2 8
$ Sum Sq : num 14.9 4.4
$ Mean Sq: num 7.47 0.55
$ F value: num 13.6 NA
$ Pr(>F) : num 0.00268 NA
Now I also have another vector of numbers that I would like to add to the
list, as a 6th variable for each element:
y <- c(.5, .7724138)
Ideally, I would like each element in the list ("Error: subject" and
"Error: subject:myfactor") to have a 6th variable, so the new str() would
look like:
List of 2
$ Error: subject :List of 1
..$ :Classes ‘anova’ and 'data.frame': 1 obs. of 5 variables:
.. ..$ Df : num 4
.. ..$ Sum Sq : num 12.4
.. ..$ Mean Sq: num 3.1
.. ..$ F value: num NA
.. ..$ Pr(>F) : num NA
* .. ..$ Thing : num 0.50*
..- attr(*, "class")= chr [1:2] "summary.aov" "listof"
$ Error: subject:myfactor:List of 1
..$ :Classes ‘anova’ and 'data.frame': 2 obs. of 5 variables:
.. ..$ Df : num [1:2] 2 8
.. ..$ Sum Sq : num [1:2] 14.9 4.4
.. ..$ Mean Sq: num [1:2] 7.47 0.55
.. ..$ F value: num [1:2] 13.6 NA
.. ..$ Pr(>F) : num [1:2] 0.00268 NA
*.. ..$ Thing : num **0.772413*
..- attr(*, "class")= chr [1:2] "summary.aov" "listof"
Now, I know how to do this if I want to just add the variable to one
element at a time:
result[[1]][[1]]$Thing <- 0.5
But I can't figure out how to do it for every element at once (other than
using a for loop). I am able to index an existing variable from each
element of a list (using lapply or sapply, based on the examples from
http://stackoverflow.com/questions/1355355/how-to-avoid-a-loop-in-r-selecting-items-from-a-listand
http://tolstoy.newcastle.edu.au/R/help/05/05/4678.html), but I can't figure
out how to *set* a new variable for each element in a list. Does anyone
know how to do this?
Thank you,
Steve Polizer-Ahles
--
Stephen Politzer-Ahles
University of Kansas
Linguistics Department
http://people.ku.edu/~sjpa/
[[alternative HTML version deleted]]
______________________________________________
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