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,"\\#"))
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.
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