Skip to content

Instantly share code, notes, and snippets.

@nilforooshan
Last active October 2, 2024 22:11
Show Gist options
  • Select an option

  • Save nilforooshan/1c615ae33caa7afa0d2fa67503118dbc to your computer and use it in GitHub Desktop.

Select an option

Save nilforooshan/1c615ae33caa7afa0d2fa67503118dbc to your computer and use it in GitHub Desktop.
R: An aggregate example

An aggregate example

(data1 <- data.frame(id=c(rep(1,3),rep(2,3)),num=1:6))
  id num
1  1   1
2  1   2
3  1   3
4  2   4
5  2   5
6  2   6
aggregate(data1$num,list(data1$id),sum)
  Group.1  x
1       1  6
2       2 15

Or

aggregate(data1$num ~ data1$id,FUN=sum)
  data1$id data1$num
1        1         6
2        2        15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment