[R] Using apply function to merge list of data frames
Berend Hasselman
bhh @end|ng |rom x@4@||@n|
Wed Jul 25 08:32:52 CEST 2018
> On 25 Jul 2018, at 08:17, Naresh Gurbuxani <naresh_gurbuxani using hotmail.com> wrote:
>
> I have a list whose components are data frames. My goal is to construct a data frame by merging all the list components. Is it possible to achieve this using apply and without a for loop, as used below?
>
> Thanks,
> Naresh
>
> mylist <- list(A = data.frame(date = seq.Date(as.Date('2018-01-01'), by = 'week',
> length.out = 5), ret = rnorm(5)),
> B = data.frame(date = seq.Date(as.Date('2018-01-01'), by = 'week',
> length.out = 5), ret = rnorm(5)))
>
> mydf <- data.frame(date = seq.Date(as.Date('2018-01-01'), by = 'week', length.out = 5))
>
> for(ch in names(mylist)){
> tempdf <- mylist[[ch]]
> names(tempdf)[2] <- paste(names(tempdf)[2], ch, sep = '.')
> mydf <- merge(mydf, tempdf, by = c('date'))}
> _
See if these would help:
on R-help the thread
https://stat.ethz.ch/pipermail/r-help/2018-May/454249.html
and
https://stackoverflow.com/questions/4512465/what-is-the-most-efficient-way-to-cast-a-list-as-a-data-frame?rq=1
Berend
More information about the R-help
mailing list