# [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 row - 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

> data1<-read.table("testR_data",na.string="0.000000")

> 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@stat.math.ethz.ch mailing list

https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!

http://www.R-project.org/posting-guide.html
Received on Tue May 03 01:43:51 2005
*
This archive was generated by hypermail 2.1.8
: Fri 03 Mar 2006 - 03:31:31 EST
*