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.
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 thenreturn> # 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 <jfearon@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]]
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
Seemingly Similar Threads
- subset() wish: allow "select" for non-dataframes
- R version 3.3.2, Windows 10: Applying a function to each possible pair of rows from two different data-frames
- R version 3.3.2, Windows 10: Applying a function to each possible pair of rows from two different data-frames
- R version 3.3.2, Windows 10: Applying a function to each possible pair of rows from two different data-frames
- How to solve a non-linear system of equations using R