Home > Enterprise >  in r dplyr select the first observation by group and create a list
in r dplyr select the first observation by group and create a list

Time:01-31

I would like to select the first observation be group and create a list column.

For example:

df <- structure(list(Grp = c("A", "A", "A", "B", "B"), col1 = c(1, 
2, 3, 70, 80), col2 = c(4, 5, 6, 100, 110)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -5L))

I would like to create a column whose first element is c(1,4) and second element is c(70, 100).

I tried:

df %>% group_by(Grp) %>% mutate(L = list(slice(1)))

But got an error.

CodePudding user response:

A possible solution:

library(dplyr)

df %>% 
  group_by(Grp) %>% mutate(L = list(c(first(col1), first(col2)))) %>% 
  ungroup

#> # A tibble: 5 × 4
#>   Grp    col1  col2 L        
#>   <chr> <dbl> <dbl> <list>   
#> 1 A         1     4 <dbl [2]>
#> 2 A         2     5 <dbl [2]>
#> 3 A         3     6 <dbl [2]>
#> 4 B        70   100 <dbl [2]>
#> 5 B        80   110 <dbl [2]>

CodePudding user response:

We could use the index:

library(dplyr)

df %>% 
  group_by(Grp) %>% 
  mutate(New_col = list(c(col1[1], col2[1]))) %>% 
  ungroup()
  Grp    col1  col2 New_col  
  <chr> <dbl> <dbl> <list>   
1 A         1     4 <dbl [2]>
2 A         2     5 <dbl [2]>
3 A         3     6 <dbl [2]>
4 B        70   100 <dbl [2]>
5 B        80   110 <dbl [2]>

OR maybe:

df %>% 
  group_by(Grp) %>% 
  mutate(New_col = toString(c(col1[1], col2[1]))) %>% 
  ungroup()
  Grp    col1  col2 New_col
  <chr> <dbl> <dbl> <chr>  
1 A         1     4 1, 4   
2 A         2     5 1, 4   
3 A         3     6 1, 4   
4 B        70   100 70, 100
5 B        80   110 70, 100

CodePudding user response:

You may use slice as -

library(dplyr)

df %>%
  group_by(Grp) %>%
  mutate(L = list(slice(cur_data(), 1))) %>%
  ungroup 

#  Grp    col1  col2 L               
#  <chr> <dbl> <dbl> <list>          
#1 A         1     4 <tibble [1 × 2]>
#2 A         2     5 <tibble [1 × 2]>
#3 A         3     6 <tibble [1 × 2]>
#4 B        70   100 <tibble [1 × 2]>
#5 B        80   110 <tibble [1 × 2]>

As slice returns a dataframe/tibble it is being saved in L column as list.

  •  Tags:  
  • Related