I have data as follows:
l = list(list(mtcars), list(mtcars))
Please note the difference with the data below
I would like to apply this solution:
# list of data frames:
l = list(mtcars, mtcars)
# vector of column names I would like to extract
my_names = c("mpg", "wt", "am")
# these columns might be at different positions in the data frames
result = lapply(l, "[", , my_names)
But the data frames in my example are a level deeper. I am having some trouble adapting the above solution to my data.
How could I adapt the syntax to get the columns from a list of listed data frames
CodePudding user response:
I have two solutions for you that could retain your nested list structure, but not sure about the efficiency.
Method 1
unlist
one layer of your list in lapply
, then index your list with my_names
and making it a list again, so that your nested list structure stays the same.
lapply(unlist(l, recursive = F), \(x) list(x[my_names]))
Method 2
Use another lapply
within lapply
to iterate the inner list.
lapply(l, \(x) lapply(x, "[", my_names))
The above two methods give the same result.
identical(lapply(unlist(l, recursive = F), \(x) list(x[my_names])),
lapply(l, \(x) lapply(x, "[", my_names)))
[1] TRUE
Output
[[1]]
[[1]][[1]]
mpg wt am
Mazda RX4 21.0 2.620 1
Mazda RX4 Wag 21.0 2.875 1
Datsun 710 22.8 2.320 1
Hornet 4 Drive 21.4 3.215 0
Hornet Sportabout 18.7 3.440 0
Valiant 18.1 3.460 0
Duster 360 14.3 3.570 0
Merc 240D 24.4 3.190 0
Merc 230 22.8 3.150 0
Merc 280 19.2 3.440 0
Merc 280C 17.8 3.440 0
Merc 450SE 16.4 4.070 0
Merc 450SL 17.3 3.730 0
Merc 450SLC 15.2 3.780 0
Cadillac Fleetwood 10.4 5.250 0
Lincoln Continental 10.4 5.424 0
Chrysler Imperial 14.7 5.345 0
Fiat 128 32.4 2.200 1
Honda Civic 30.4 1.615 1
Toyota Corolla 33.9 1.835 1
Toyota Corona 21.5 2.465 0
Dodge Challenger 15.5 3.520 0
AMC Javelin 15.2 3.435 0
Camaro Z28 13.3 3.840 0
Pontiac Firebird 19.2 3.845 0
Fiat X1-9 27.3 1.935 1
Porsche 914-2 26.0 2.140 1
Lotus Europa 30.4 1.513 1
Ford Pantera L 15.8 3.170 1
Ferrari Dino 19.7 2.770 1
Maserati Bora 15.0 3.570 1
Volvo 142E 21.4 2.780 1
[[2]]
[[2]][[1]]
mpg wt am
Mazda RX4 21.0 2.620 1
Mazda RX4 Wag 21.0 2.875 1
Datsun 710 22.8 2.320 1
Hornet 4 Drive 21.4 3.215 0
Hornet Sportabout 18.7 3.440 0
Valiant 18.1 3.460 0
Duster 360 14.3 3.570 0
Merc 240D 24.4 3.190 0
Merc 230 22.8 3.150 0
Merc 280 19.2 3.440 0
Merc 280C 17.8 3.440 0
Merc 450SE 16.4 4.070 0
Merc 450SL 17.3 3.730 0
Merc 450SLC 15.2 3.780 0
Cadillac Fleetwood 10.4 5.250 0
Lincoln Continental 10.4 5.424 0
Chrysler Imperial 14.7 5.345 0
Fiat 128 32.4 2.200 1
Honda Civic 30.4 1.615 1
Toyota Corolla 33.9 1.835 1
Toyota Corona 21.5 2.465 0
Dodge Challenger 15.5 3.520 0
AMC Javelin 15.2 3.435 0
Camaro Z28 13.3 3.840 0
Pontiac Firebird 19.2 3.845 0
Fiat X1-9 27.3 1.935 1
Porsche 914-2 26.0 2.140 1
Lotus Europa 30.4 1.513 1
Ford Pantera L 15.8 3.170 1
Ferrari Dino 19.7 2.770 1
Maserati Bora 15.0 3.570 1
Volvo 142E 21.4 2.780 1
CodePudding user response:
One possible way to solve your problem:
result = lapply(l, function(x) x[[1L]][my_names])
[[1]]
mpg wt am
Mazda RX4 21.0 2.620 1
Mazda RX4 Wag 21.0 2.875 1
Datsun 710 22.8 2.320 1
Hornet 4 Drive 21.4 3.215 0
Hornet Sportabout 18.7 3.440 0
Valiant 18.1 3.460 0
...
[[2]]
mpg wt am
Mazda RX4 21.0 2.620 1
Mazda RX4 Wag 21.0 2.875 1
Datsun 710 22.8 2.320 1
Hornet 4 Drive 21.4 3.215 0
Hornet Sportabout 18.7 3.440 0
Valiant 18.1 3.460 0
...