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

Reply via email to