Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide a data.frame into seversal data.frames according to some specail character?

Tags:

dataframe

r

How to divide a data.frame into seversal data.frames according to some specail character?

df <- tibble(NumberA = c(5,3,2,0,"\\#",2,0,"\\#",3,1,1,3,1,0,"\\#"),
             NumberB = c(5,6,2,5,"\\#",4,3,"\\#",4,3,2,1,3,9,"\\#"))

the new generated data.frames

like image 470
AdIan Avatar asked Dec 30 '22 22:12

AdIan


1 Answers

Option 1

A base one-liner :

split(df, replace(cumsum(df$NumberA == "\\#"), df$NumberA == "\\#", NA))

Option 2

A dplyr solution with group_split().

library(dplyr)

df %>%
  group_by(grp = cumsum(NumberA == "\\#")) %>%
  filter(NumberA != "\\#") %>%
  group_split(.keep = FALSE)

Output

# [[1]]
# # A tibble: 4 x 2
#   NumberA NumberB
#   <chr>   <chr>  
# 1 5       5      
# 2 3       6      
# 3 2       2      
# 4 0       5      
# 
# [[2]]
# # A tibble: 2 x 2
#   NumberA NumberB
#   <chr>   <chr>  
# 1 2       4      
# 2 0       3      
# 
# [[3]]
# # A tibble: 6 x 2
#   NumberA NumberB
#   <chr>   <chr>  
# 1 3       4      
# 2 1       3      
# 3 1       2      
# 4 3       1      
# 5 1       3      
# 6 0       9

Update

If you wanna get the mean of each column in each data.frame and combine all the means into one data.frame, you can use map_dfr() in purrr.

library(purrr)

map_dfr(df_split, ~ colMeans(mutate(.x, across(everything(), as.numeric))))

# # A tibble: 3 x 2
#   NumberA NumberB
#     <dbl>   <dbl>
# 1     2.5    4.5 
# 2     1      3.5 
# 3     1.5    3.67

where df_split is the splitted data.

like image 54
Darren Tsai Avatar answered May 12 '23 18:05

Darren Tsai