You have to return a value from the function in the lapply and assign the result to another object:
> df <- data.frame(a=1,b=2,c=3,d=4) > a <- list(df,df,df,df) > # to change the name of the second, you have to change the name and then return > # the dataframe as the return value and assign it back into another list > a.1 <- lapply(a, function(x){ + names(x)[2] <- 'newName' + x # return + }) > > a [[1]] a b c d 1 1 2 3 4 [[2]] a b c d 1 1 2 3 4 [[3]] a b c d 1 1 2 3 4 [[4]] a b c d 1 1 2 3 4 > a.1 [[1]] a newName c d 1 1 2 3 4 [[2]] a newName c d 1 1 2 3 4 [[3]] a newName c d 1 1 2 3 4 [[4]] a newName c d 1 1 2 3 4 > On Mon, May 25, 2009 at 6:19 PM, James Fearon <jfea...@stanford.edu> wrote: > Hi, > > Say I have dataframes d1, d2, ... , dn, and I want to apply a function to > all of them. For example, say I want to change the name of the second > variable in each dataframe to "x2". The following doesn't work: > > a = list(d1,d2,d3,d4) > lapply(a,function(x) names(x)[2] = "x2") > > What would work? > > Thanks for any help. > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[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.