Re: [R] "mystic" apply ?

About this list Date view Thread view Subject view Author view Attachment view

From: Thomas Lumley (tlumley@u.washington.edu)
Date: Wed 11 Dec 2002 - 07:30:56 EST


Message-id: <Pine.A41.4.44.0212101226520.42930-100000@homer31.u.washington.edu>

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@stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
>

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

______________________________________________
R-help@stat.math.ethz.ch mailing list
http://www.stat.math.ethz.ch/mailman/listinfo/r-help


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Tue 01 Jun 2004 - 09:31:51 EST