-2

Suppose that I have the following dataframe

data = data.frame(age=c("a","a","b","b"),cause=c("one","two","one","two"),value=c(1,2,5,8))
data
   age cause value
1   a   one     1
2   a   two     2
3   b   one     5
4   b   two     8

I'd like to create a new cause summing the values of causes for both ages, as we can see bellow

data
   age cause value
1   a   one     1
2   a   two     2
3   b   one     5
4   b   two     8
5   a   onetwo  3
6   b   onetwo  13

Any idea of how can I do this?

DR15
  • 647
  • 9
  • 17
  • See [how-to-sum-a-variable-by-group](https://stackoverflow.com/questions/1660124/how-to-sum-a-variable-by-group), and `rbind` the output of your sum-by-group operation to `data` – IceCreamToucan Nov 25 '19 at 18:02

3 Answers3

1

One option is to use dplyr:

library(dplyr)
data <- data.frame(age=c("a","a","b","b"),cause=c("one","two","one","two"),value=c(1,2,5,8),
                  stringsAsFactors = FALSE)

data <- bind_rows(data, data %>% group_by(age) %>%
                               summarise(cause = paste0(cause, collapse = ""),
                                                        value = sum(value)))
slava-kohut
  • 4,203
  • 1
  • 7
  • 24
0

An option with data.table

library(data.table)
rbind(setDT(data), data[, .(cause = paste(unique(cause), collapse=''), 
      value = sum(value)), .(age)])
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Here is my own approach, according to the reponse that was deleted a minute ago, I don't know why.

data = data.frame(age=c("a","a","b","b"),cause=c("one","two","one","two"),value=c(1,2,5,8))

data_age = aggregate(value~ age + cause, data, sum)
causes = aggregate(value ~ age, data, sum)
causes$cause = "Both"
data_age <- rbind(data_age, causes)
DR15
  • 647
  • 9
  • 17