[R] Crosstab with Average and Count

vioravis vioravis at gmail.com
Fri Jul 20 12:30:16 CEST 2012


I have the following data:

x <- as.factor(c(1,1,1,2,2,2,3,3,3))
y <- as.factor(c(10,10,10,20,20,20,30,30,30))
z <- c(100,100,NA,200,200,200,300,300,300)

I could create the cross tab of x and y with Sum of z as its elements using
the xtabs function as follows:

# X Vs. Y with Sum Z

xtabs(z ~ x + y)

   y
x    10  20  30
  1 200   0   0
  2   0 600   0
  3   0   0 900

How do I replace the sum with average and count so that I can get the
following outputs??

# X Vs. Y with Average of Z
   y
x      10  20  30
  1    100 0   0
  2    0   200 0
  3    0   0   300

# X Vs. Y with Count Z
  y
x    10  20  30
 1    2   0   0
 2    0   3   0
 3    0   0   3

Would appreciate any help on these? Thank you.

Ravi





--
View this message in context: http://r.789695.n4.nabble.com/Crosstab-with-Average-and-Count-tp4637180.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list