[R] Assigning into each of a list of dataframes

Chuck Cleland ccleland at optonline.net
Fri Jan 18 16:37:09 CET 2008


   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



More information about the R-help mailing list