将一个数据帧拆分为几个数据帧 [英] Splitting a dataframe into several dataframes
问题描述
我有一个数据框,需要根据正则表达式搜索将其分为几个数据框。搜索没有固定的模式,即有时只有一个正则表达式,有时是多个正则表达式的组合。这是一个仅提取了一组行的最小示例:
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.
我正在寻找一个单行命令来执行此操作。帮助表示赞赏,尤其是在使用 dplyr
的情况下。
I am looking for a single line command to do this. Help appreciated, especially if using dplyr
.
推荐答案
我们可以编写一个函数,如
We can write a function like
split_df <- function(df, char) {
split(df, grepl(char, df$Name))
}
new_df <- split_df(main_df, "J")
new_df[[1]]
# Name Age City
#3 Arthur 31 New York
#4 Maggie 33 Delhi
new_df[[2]]
# Name Age City
#1 John 20 London
#2 Jane 30 Paris
代替 char
确保传递适当的字符以进行分割。您还可以对 char
使用正则表达式,例如 ^ J
(以J开头)或 J $
(以J结尾)等。
In place of char
make sure to pass appropriate character to split on. You can also use regex for char
like ^J
(starts with J) or J$
(ends with J) etc.
例如
new_df <- split_df(main_df, "^J")
与上面的输出相同。
这篇关于将一个数据帧拆分为几个数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!