[R] How to modify data frame stored in a list

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Mon Jun 18 23:53:00 CEST 2018

It depends on whether you wish to refer to the column to be logged by name
or index.


dat <- lapply(1:3, function(i)data.frame(a = runif(5), b =

## by numerical index of column
d <- lapply(dat,function(x){x[,"logged"]<- log10(x[,1]); x})

## by name of column
dd <- lapply(dat,function(x){x[,"logged"]<- log10(x[,"a"]); x})

There are also slight variations on how you can do the "[" indexing that
others may post.
Note that you have to return the modified data frame in the function.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Mon, Jun 18, 2018 at 1:46 PM, Honkit Wong <stephen66 using gmail.com> wrote:

> Dear R community,
> I have a question seems very simple but have trouble to do it.
> I have a list which stores many data frames. Now, I want to perform log10
> on one column in each data frame in the list and save the value as a new
> column back to the original data frame in the list. How do I quickly do
> that with lapply function ?
> Many thanks.
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

	[[alternative HTML version deleted]]

More information about the R-help mailing list