# [R] newbie ifelse matrix question

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Mon May 2 17:54:54 CEST 2005

```try something like this:

dat <- matrix(rnorm(20*205), 20, 205)
sds <- sd(t(dat))
######
dat[, 6:205][abs(dat[, 6:205]) > sds] <- NA

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm

----- Original Message -----
From: "Christine Krisky" <kriskyc at ohsu.edu>
To: <r-help at stat.math.ethz.ch>
Sent: Monday, May 02, 2005 5:13 PM
Subject: [R] newbie ifelse matrix question

> Hi all,
>
> I have time series data in a matrix format 20 rows x 205 columns and
> have been trying to replace outliers with NA.  My first column
> contains the outliers threshold (3 Standard deviations) for each
> ow  - here's a bit of the first row
>
>   sd3      V1 V2 V3 V4         V5        V6        V7        V8
> V9
> 1  13.03267 1797157 75 84 58  -1.958649  0.048775  2.056198
> 8.063622  3.071045
>
> What I want is a statment that says if the absolute value of
> [,5:205]  <= column 1 keep the value, otherwise replace with NA
>
> I've tried this without success - I do know this line works with
> s-plus
> data1[,6:205]<-ifelse(abs(data1[,6:205])<=data1[,1],data1[,6:205],
> NA)
>
> But what I get with R is only partially correct.  I get NA in places
> where there shouldn't be.  About every 7 columns or so I end up with
> 3 columns of NA and no replacement with NA where there should be
> (V11) in other places.  The majority of the matrix though is
> correct.
>
>   sd3      V1     V2 V3 V4     V5        V6        V7        V8
> V9         V10      V11     V12  V13 V14 V15
> 1  13.03267 1797157 75 84 58  -1.958649  0.048775  2.056198
> 8.063622  3.071045  0.078468   -21.9141    NA   NA   NA  3.115585
>
>
> I've tried searching the manual and user list and countless changes
> to the syntax over the last week, but no luck so far.
>
> Here is my syntax and error
>
>> dim(data1)
> [1]  20 204
> # Find sd of rows, then multiply * 3
>> sd_data3<-apply(data1[,5:204],1,sd,na.rm=TRUE)*3
> # Attach sd_data3 to data1
>> sd_and_data1<-cbind(sd_data3,data1)
> # Replace values >= 3 SDs with NA
>> sd_and_data1[,6:205]<-ifelse(abs(sd_and_data1[,6:205])<=sd_and_data1[,1],sd_and_data1[,6:205],
>> NA)
> Warning message:
> provided 4000 variables to replace 200 variables in:
> "[<-.data.frame"(`*tmp*`, , 6:205, value = list(c(-1.958649,
>
> I can't figure this out and would be very grateful for your help.
>
> -chris
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help