4

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?

user213544
  • 2,046
  • 3
  • 22
  • 52
Bing
  • 57
  • 4
  • 1
    Assuming the datasets are derived from many files with the same structure, I would suggest reading the files into one dataframe, with a column to indicate the file. See [this question and answers](https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once), for example. – neilfws Jan 28 '20 at 04:15

2 Answers2

5

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)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

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)
akrun
  • 874,273
  • 37
  • 540
  • 662