[R] How to extract same columns from identical dataframes in a list?

Wolfgang Waser
Mon, 08 Feb 2016 13:33:21 -0000

Hello,

I have a list of 7 data frames, each data frame having 24 rows (hour of
the day) and 5 columns (weeks) with a total of 5 x 24 values

I would like to combine all 7 columns of week 1 (and 2 ...) in a
separate data frame for hourly calculations, e.g.
> apply(new.data.frame,1,mean)

In some way sapply (lapply) works, but I cannot directly select columns
of the original data frames in the list. As a workaround I have to
select a range of values:

> sapply(list_of_dataframes,"[",1:24)

Values 1:24 give the first column, 25:48 the second and so on.

Is there an easier / more direct way to select for specific columns
instead of selecting a range of values, avoiding loops?


Cheers,

Wolfgang