Home > Mobile >  Create column with vector with all rows indices within group with Dplyr/R
Create column with vector with all rows indices within group with Dplyr/R

Time:12-21

Let's say my data is as follows:

index, task
1, 1
2, 1
3, 1
4, 2
5, 2
6, 2

How can I make a dataframe like this:

task, indices
1, c(1,2,3)
2, c(4,5,6)

I want to essentially create something like a dictionary lookup for each task such that we can get its indices in the original dataframe with an O(1) operation.

CodePudding user response:

Use aggregate in base R

aggregate(.~ task, data, list)
  task   index
1    1 1, 2, 3
2    2 4, 5, 6

CodePudding user response:

I don't if this will help, but you could achieve this

Code

library(dplyr)
library(purrr)

data <-
  data.frame(
    index = 1:6,
    task = rep(1:2, each = 3)
  )

data %>% 
  group_by(task) %>% 
  summarise(indices = list(reduce(index,c)))

Output

# A tibble: 2 x 2
   task indices  
  <int> <list>   
1     1 <int [3]>
2     2 <int [3]>
  • Related