I have a list of data frames. I'd like to extract the first 248 rows of every data frame, then bind them together into one single data frame.
allData is the list of data frames. I've tried to use lapply with subset but I don't know how to refer to row numbers in there. It seems the "subset" argument of the subset function only accept logical vector.
temp <- lapply(allData, subset, subset = (row.names(allData) <= 248))
This code is my idea which does not work, because row.names(allData) actually only applies to the list itself, not the data frames. How to refer to the row numbers of each data frame then?
Using base R, we can do
do.call(rbind, lapply(allData, function(x) x[1:248, ]))
OR with purrr
purrr::map_dfr(allData, ~.[1:248, ])
Using reproducible example with mtcars data and subsetting first 5 rows.
allData <- list(mtcars, mtcars)
do.call(rbind, lapply(allData, function(x) x[1:5, ]))
# mpg cyl disp hp drat wt qsec vs am gear carb
#Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#Mazda RX41 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#Mazda RX4 Wag1 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#Datsun 7101 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#Hornet 4 Drive1 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#Hornet Sportabout1 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With