1

I want to attach the values of a group together, here is the sample:

data<-data.table(group=c("g1","g1","g2","g2","g1"),value=c(1,2,3,4,5))

my desired output would be:

     group  value
1:    g1     125
2:    g2     34

Values could be treated as numbers or characters.

ekad
  • 14,436
  • 26
  • 44
  • 46
Soheil
  • 954
  • 7
  • 20

1 Answers1

1

In addition to the data.table solution from thelatemail, you can do this in base R:

aggregate(value ~ group, data, FUN = function(x) paste(x, collapse = ""))

Or with dplyr:

data %>%
  group_by(group) %>%
  summarise(value = paste(value, collapse = ""))
Sam Firke
  • 21,571
  • 9
  • 87
  • 105