Home > Software design >  How to remove the grouped rows from the original data frame and put them in new one?
How to remove the grouped rows from the original data frame and put them in new one?

Time:01-14

I want to remove the rows which had been grouped from the original data frame and put them into another data frame.

library(dplyr)

Name <- c("Jon", "Jon", "Jon", "Jon", "Jon", "Jon")
school <- c("a", "a", "b", "c", "x", "y")
Age <- c(10, 15, 20, 25, 30, 60)

df <- data.frame(Name, school, Age )

#case_1
dfAvg <- df %>%
group_by(Name, school) %>% 
summarise(across(Age, mean))

I want after this to remove the rows that resulted from group by so my original Df has 4 rows and a new Df has only 1 row that came out from group by.

expected output: 2 data frames: the first one:

jon b 20
jon c 25
jon x 30
jon y 60

and the second one contains the row that resulted after group by:

jon a 12.5

CodePudding user response:

Here is one way that stores your 2 data frames in a list

ibrary(dplyr)
df %>% 
 group_by(Name, school) %>% 
 summarise(n = n() > 1, Age = mean(Age)) %>% 
 split(., .$n)

`summarise()` has grouped output by 'Name'. You can override using the 

$`FALSE`
# A tibble: 4 x 4
# Groups:   Name [1]
  Name  school n       Age
  <chr> <chr>  <lgl> <dbl>
1 Jon   b      FALSE    20
2 Jon   c      FALSE    25
3 Jon   x      FALSE    30
4 Jon   y      FALSE    60

$`TRUE`
# A tibble: 1 x 4
# Groups:   Name [1]
  Name  school n       Age
  <chr> <chr>  <lgl> <dbl>
1 Jon   a      TRUE   12.5

CodePudding user response:

Or you can use the function slice from dplyr package to get the two outputs :

#First data frame :
dfAvg %>% dplyr::slice(-1)
#or
dfAvg %>% dplyr::slice(2:5)

# A tibble: 4 x 3
# Groups:   Name [1]
  Name  school   Age
  <fct> <fct>  <dbl>
1 Jon   b         20
2 Jon   c         25
3 Jon   x         30
4 Jon   y         60

#Second data frame :
dfAvg %>% dplyr::slice(1)

# A tibble: 1 x 3
# Groups:   Name [1]
  Name  school   Age
  <fct> <fct>  <dbl>
1 Jon   a       12.5
  •  Tags:  
  • Related