I have a dataframe that I need split into several dataframes, based on regex searches. There is no set pattern to the searches, i.e. sometimes there is a single regex, sometime a combination of several. Here is a minimal example with just one set of rows extracted:
Name <- c("John", "Jane", "Arthur", "Maggie")
Age <- c(20, 30, 31, 33)
City <- c("London", "Paris", "New York", "Delhi")
main_df <- data.frame(Name, Age, City)
sub_df <- main_df %>%
filter(grepl("J", Name))
main_df <- main_df %>%
filter(!grepl("J", Name))
Note that I am extracting some rows into a new dataframe, then deleting the extracted rows from the main dataframe.
I am looking for a single line command to do this. Help appreciated, especially if using dplyr.