The problem is that your function is returning x$Y rather than x so the result is a list of Y's rather than a list of the x's with Y added. You want function(x) { x$Y <- x$A * x$B; x } or a bit shorter:
lapply(mylist, transform, Y = A * B) On Jan 18, 2008 10:37 AM, Chuck Cleland <[EMAIL PROTECTED]> wrote: > What is the right way to assign a new variable into each a of list of > data frames? Here is my failed attempt: > > mylist <- list(df1 = data.frame(A = runif(5), B = runif(5)), > df2 = data.frame(A = runif(5), B= runif(5))) > > lapply(mylist, function(x){x$Y <- x$A * x$B}) > > $df1 > [1] 0.25589928 0.03446026 0.94992362 0.21388326 0.08668821 > > $df2 > [1] 0.08771839 0.05643553 0.09036894 0.09179378 0.37394748 > > mylist > $df1 > A B > 1 0.3293760 0.77692140 > 2 0.1283307 0.26852710 > 3 0.9865388 0.96288517 > 4 0.5087024 0.42044870 > 5 0.9175345 0.09447951 > > $df2 > A B > 1 0.3887178 0.2256608 > 2 0.2642189 0.2135938 > 3 0.3881635 0.2328115 > 4 0.9060760 0.1013091 > 5 0.4578424 0.8167602 > > I want the variable Y to be added to each data frame in mylist. > > thanks, > > Chuck > > -- > Chuck Cleland, Ph.D. > NDRI, Inc. > 71 West 23rd Street, 8th floor > New York, NY 10010 > tel: (212) 845-4495 (Tu, Th) > tel: (732) 512-0171 (M, W, F) > fax: (917) 438-0894 > > ______________________________________________ > 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. > ______________________________________________ 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.