Sunday, 15 January 2012

apply - Finding sum in all rows with R -



apply - Finding sum in all rows with R -

i have 2 vectors

x <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,4,4,4,5,5,6,6,6,6) y <- c(1,1,2,3,4,2,2,4,4,4,3,3,1,4,2,3,1,4,4,4,2,2,2,3,3)

i found number of values each x (from 1 6)

t=table(x,y)

and table 6 rows , 4 columns. calculate sum in rows s=apply(t,1,sum) , error. explain wrong?

what error? don't 1 apply(t, 1, sum). seek instead

rowsums(t) ##1 2 3 4 5 6 ##5 5 4 5 2 4

or, utilize table(x), gives same output.

r apply

No comments:

Post a Comment