table - Conditional calculating the numbers of values in column with R, part2 -
table - Conditional calculating the numbers of values in column with R, part2 -
i have 3 vectors:
x <- c(1,1,1,1,1, 2,2,2,3,3, 3,3,3,4,4, 5,5,5,5,5 ) y <- c(2,2,1,3,2, 1,4,2,2,na, 3,3,3,4,na, 1,4,4,2,na) w <- c(1,45,na,45,na,45,41,45,96,25,12,na,7,na,4,45,12,45,32,56)
how can find number of values in w (don`t count na) each x (from 1 5) , each y (from 1 4)?
the output should in format like:
y x result 4 1 ... 4 2 ... 4 3 4 4 4 5 3 1 3 2 3 3 3 4 3 5 ... 1 1 1 2 1 3 1 4 1 5
here can utilize xtabs sum values w
not na
dd<-as.data.frame(xtabs(!is.na(w)~y+x), stringsasfactors=f)
the as.data.frame
part changes form table long format desire. grab xtabs
converts x
, y
characters. can convert them numeric with
dd$x <- as.numeric(dd$x) dd$y <- as.numeric(dd$y)
and sort with
dd <- dd[order(-dd$y,dd$x),]
r table aggregate apply
Comments
Post a Comment