I have two 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)
This question (Conditional calculating the numbers of values in column with R, part2) discussed how to find the number of values in w
(don't count NA
) for each x
(from 1–5) and for each y
(from 1–4).
Let's split X
by groups: if x<=2
, group I
; if 2<x<=3
, group II
; and if 3<X<=5
, group III
. I need to find the number of different values in x
by groups and by every value of y
. I also need to find the mean of those values in x
by the same groups. The output should be in this format:
y x Result 1 (the number of distinct numbers in X); Result 2 (the mean)
1 I ...
1 II ...
1 III ...
...
4 I ...
4 II ...
4 III ...
If you'd like the results to be
NA
whenNA
s are present, then just removena.rm=TRUE
frommean(.)
:My command of R code isn't great, so here's A Rather Ugly Function:
With your
x
andy
,ARUF(x,y)
prints thisdata.frame
:I went a little out of my way to make
ARUF
robust with any integer values ofy
. I can't seem to break it by generatingy
randomly withrbinom
, and I believe it should handle any real number values ofx
, so it should work for any other vectors of the same kind that you might have.