[R] Summarizing a matrix

Rui Barradas ruipbarradas at sapo.pt
Tue Jun 5 18:13:34 CEST 2012


Hello,

Better yet is function aggregate.

# Create some data matrix
Y <- matrix(c(sample(80, 507, TRUE), sample(-1:1, 4*507, TRUE)), ncol=5)

X <- aggregate(Y[, 5]~Y[, 1], Y, sum) # In your case Y[, 4] not Y[, 1]


Hope this helps,

Rui Barradas

Em 05-06-2012 09:53, Özgür Asar escreveu:
> Hi,
>
> x<-matrix(0,80,ncol=1)
>
> will create x matrix with all elements 0 (to be filled by the sums that you
> need)
>
> sum(y[y[,4]==1,5])
>
> will calculate the sum of 5th column of y with 4th column=1
>
> Similarly,
>
> sum(y[y[,4]==80,5])
>
>   will calculate the sum of 5th column of y with 4th column=80.
>
> You can adapt this to your case, with simple loops, etc.
>
> Hope this helps.
> Ozgur
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Summarizing-a-matrix-tp4632319p4632381.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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