Hi Arun,

Thanks a lot for the help. I think I didn't make my request clear, though;
sorry about that. What your example does is applies the values of y to each
element in the list "result". However, what I'm actually trying to do is
apply the nth element of y to the nth element of result. In other words,
I'm trying to find a way in one command to do the equivalent of the
following:

result[[1]][[1]]$Thing <- y[1]
result[[2]][[1]]$Thing <- y[2]
...etc.

Is this even possible? (Granted, for my purpose "result" is not a very long
list, so it would not be difficult to just do those commands one at a time
or in a for loop. At this point I'm just curious to figure out a more
straightforward way to do it just for the sake of trying to learn more
about how R works!)

If it helps put things into context, in my example the list "result" is a
summary of an aov object, and each element in the list is one of the ANOVA
effects; y is a vector of the partial eta squareds I calculated
corresponding to each effect, and I'm trying to figure out a way to add
those into the "result" object so it shows up when my colleague looks at
the summary.

Best,
Steve

On Sat, Nov 24, 2012 at 5:22 PM, arun <smartpink111@yahoo.com> wrote:

> 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@gmail.com>
> To: r-help@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@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.
>
>


-- 
Stephen Politzer-Ahles
University of Kansas
Linguistics Department
http://people.ku.edu/~sjpa/

	[[alternative HTML version deleted]]


