[R] Create subsets of data
David Winsemius
dwinsemius at comcast.net
Mon May 9 21:46:15 CEST 2011
On May 9, 2011, at 9:40 AM, Pavan G wrote:
> Hello All,
> Let's say I have points on a x-y plane. x ranges from 0-2 and y from
> 0-2.
> There are points in quadrants x[0:1]---y[0:1] and in x[1:2]----
> y[1:2]. I
> would like to get the mean and std of the points in the x[0:1]----
> y[0:1]
> quadrant alone. Is there a straight forward way to do it?
I am assuming that you have yet a third vector that has values at each
of those coordinates and it is that set of categorized values for
which you want the means and std deviations: I will exemplify the use
of tapply on cut()-igorized vectors and a z-value which is the L1
distance from the origin. (And excuse the earlier feeble attempt at
humor);
> x <- runif(2000, 0,2)
> y <- runif(2000, 0, 2)
> xc <- cut(x, c(0,1,2))
> yc <- cut(y, c(0,1,2))
> z <- x+y
> tapply(z, list(xc,yc), mean)
(0,1] (1,2]
(0,1] 1.013192 2.016095
(1,2] 1.979930 2.996229 # seems to make sense
> tapply(z, list(xc,yc), sd)
(0,1] (1,2]
(0,1] 0.4028310 0.4133113
(1,2] 0.4239014 0.3984559 # also seems sensible
>
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list