[R] apply fn to many dataframes
markleeds at verizon.net
markleeds at verizon.net
Tue May 26 01:03:56 CEST 2009
Hi:Â you have to return the dataframe inside the lapply. I also changed =
to <- but I doubt that matters.
d1 <- data.frame(x1=1,x3=4)
d2 <- data.frame(x1=2,x3=5)
d3 <- data.frame(x1=3,x3=6)
d4 <- data.frame(x1=4,x3=7)
a = list(d1,d2,d3,d4)
print(a)
lapply(a,function(.df) {
   names(.df)[2] <- "x2"
   .df
})
On May 25, 2009, James Fearon <jfearon at 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.
______________________________________________
[1]R-help at r-project.org mailing list
[2]https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
[3]http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
References
1. mailto:R-help at r-project.org
2. https://stat.ethz.ch/mailman/listinfo/r-help
3. http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list