[R] "mystic" apply ?

Thomas Lumley tlumley at u.washington.edu
Tue Dec 10 21:32:02 CET 2002


On Tue, 10 Dec 2002, Christian Schulz wrote:

> hi,
>
> have anybody a suggestion why this function
> works only for a vector correct.
> When i'm using apply(data,2,fuzzy) i have the columns
> stacked (i.e. dim(x) =1000,4  should after the function 1000,8
> instead is 2000,4) - for single vector it's ok.
>

No, apply doesn't do that -- it doesn't look at matrix arguments at all.

If dim(x) is c(1000,4) then fuzzy is called 4 times, each time with an
argument of length 1000.  It returns a value of length 2000 and so you get
a 2000x4 result.

I can't see why you would expect to get a 1000x8 result -- I might have
expected a 1000x4x2 array, though.

You can get the result you want with

matrix(apply(x,2,fuzzy), nrow=nrow(x))


	-thomas


> fuzzy  <- function (x)
> {
>     fuz.x <- cbind(x, x)
>     min <- quantile(x, 0.20)
>     max <- quantile(x, 0.80)
>     fuz.x[x >= max, 1] <- 1
>     fuz.x[x >= max, 2] <- 0
>     fuz.x[x <= min, 1] <- 0
>     fuz.x[x <= min, 2] <- 1
>     fuz.x[x > min & x < max, 1] <- (x[x > min & x < max] - min)/(max - min)
>     fuz.x[x > min & x < max, 2] <- (max - x[x > min & x < max])/(max - min)
>     fuz.x
> }
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
>

Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle
^^^^^^^^^^^^^^^^^^^^^^^^
- NOTE NEW EMAIL ADDRESS




More information about the R-help mailing list