0

I have a data frame generated by

points_A = sample(1:6,6)
points_B = sample(1:6,6)
points_C = sample(1:6,6)
df <- data.frame( name = gl(3,2,labels=c("Luca","Mario","Paolo") ) , cbind(points_A,points_B,points_C)  )

which display as

   name points_A points_B points_C
1  Luca        5        2        3
2  Luca        3        3        1
3 Mario        1        5        2
4 Mario        6        6        4
5 Paolo        4        4        5
6 Paolo        2        1        6

I would like to apply a function (e.g. sum() ) to the rows grouped by the column name (1st column).

The output should be something like:

   name points_A points_B points_C
1  Luca        8        5        4
2  Mario       7       11        6
3  Paolo       6        5       11

Any suggestions?

alevax
  • 80
  • 1
  • 8
  • 1
    `aggregate(. ~ name, df, sum)`, or in dplyr, `df %>% group_by(name) %>% summarise_all(sum)` – alistaire Oct 10 '16 at 23:11
  • 1
    `aggregate(. ~ name, df, sum)` this is working, but if I've other columns like lastname, city etc, that I still want in the dataframe but I do not want to pass to aggregate because they are another data type? Is there a way to specify what keep out? – alevax Oct 11 '16 at 00:09
  • 1
    [This solved my previous comment](http://stackoverflow.com/questions/15978985/how-to-aggregate-some-columns-while-keeping-other-columns-in-r) – alevax Oct 11 '16 at 00:09

2 Answers2

0

I like to do these things with data.table

library(data.table); dt<-data.table(df) ; dt[, function(column), by = group] As "column" you can also set .SD to get multiple columns. "group" would be "name" in your example.

Daniel Winkler
  • 487
  • 3
  • 11
0

A (pretty raw) solution with data.table

require(data.table)
setDT(df)
df[, lapply(.SD, sum), by = name, .SDcols = 2:4]

    name points_A points_B points_C
1:  Luca        9        6        6
2: Mario        5       10       11
3: Paolo        7        5        4

EDIT: A raw solution in base R:

t(sapply(split(df, df$name), function(x) colSums(x[, c("points_A", "points_B", "points_C")])))
s_baldur
  • 29,441
  • 4
  • 36
  • 69