I have 100 data sets about data for 100 different locations.
I want to get the subset of the same variables for each data set. Something like:
dataset1<-subset(dataset1, which(gender=='F'))
dataset2<-subset(dataset2, which(gender=='F'))
dataset3<-subset(dataset3, which(gender=='F'))
dataset4<-subset(dataset4, which(gender=='F'))
.....
How can I get all 100 data sets done at the same time instead of writing 100 lines?
You can put the dataset in a list and then use subset
on each using lapply
list_df <- lapply(mget(paste0('dataset', 1:100)),function(x) subset(x, gender=='F'))
It is better to keep data in a list but if needed as separate dataframes, we can use list2env
list2env(list_df,.GlobalEnv)
We can use map
from purrr
library(dplyr)
library(purrr)
library(stringr)
list_df <- mget(str_c("dataset", 1:100)) %>%
map(~ .x %>%
filter(gender == "F"))
list2env(list_df, .GlobalEnv)
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