On Feb 3, 2012, at 6:17 PM, brann...@mskcc.org wrote:

Hello,

I'm trying to replace any value within a column where the value is less than 10% of the median of the column with NA. In other words, if the median of one column is 500, any value in that column that is less than 50 should become NA.

Doing a lot of searches, it seems like I should be using apply. But when I put the if statement inside like this, I get serious errors:

You didn't show str(gc2) or show us how it might be built.


test<-apply(gc2,2,function(x){if(x<(0.1*median(x))), NA})
Error: unexpected ',' in "test<-apply(gc2,2,function(x) {if(x<(0.1*median(x))),"

You want ifelse() rather than if(). You also will need to look more carefully at how 'ifelse' is designed.


test<-apply(gc2,2,function(x){if(x<(0.1*median(x))) NA})
There were 50 or more warnings (use warnings() to see the first 50)
In if (x < (0.1 * median(x))) x <- NA :
 the condition has length > 1 and only the first element will be used

Trying
test<-apply(gc2,2,function(x){x[x<(0.1*median(x))]<- NA})
head(test)
NA.01.N NA.01.T NA.02.N NA.02.T NA.03.N NA.03.T
    NA      NA      NA      NA      NA      NA

That probably indicates that you need to look more carefully at ? median because it appears you have at least one NA in each of your gc2 columns.



I'm sure that I could get it to work if I read each column individually into a vector, calculate the median, replace the values if less than 0.1*median, then rebind it into a new matrix. However, I'm hoping there is a more elegant way.

Perhaps the sweep function?

        [[alternative HTML version deleted]]

You should read the Posting Guide and learn how to post plain text.


David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to