[R] question about sum of (column) elements in R

Pascal Oettli kridox at ymail.com
Tue Dec 4 09:37:35 CET 2012


Hi,

You can follow this example:

test <- structure(list(V1 = c(0L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), V2 = c(12L, 10L, 4L, 6L,
7L, 13L, 21L, 23L, 20L, 18L, 17L, 16L, 27L, 33L, 11L, 8L, 19L,
16L, 9L)), .Names = c("V1", "V2"), class = "data.frame", row.names = 
c(NA,-19L))

tapply(grid1$V2, grid1$V1, sum)

   0   1   2   3   4
  12  40  64 111  63

HTH
Pascal


Le 04/12/2012 16:59, T Bal a écrit :
> Hi,
> I have the following data:
>
> 0   12
> 1   10
> 1   4
> 1   6
> 1   7
> 1   13
> 2   21
> 2   23
> 2   20
> 3   18
> 3   17
> 3   16
> 3   27
> 3   33
> 4   11
> 4   8
> 4   19
> 4   16
> 4   9
>
>
> In this data file I would like to sum the numbers of second column which
> belong to the same number in the first column.
> So the output would be:
>
> 0     12
> 1     40
> 2     64
>          3     111
>          etc.
>
> Thank you.
>
> Kind regards,
> T. Bal
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list