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:> 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))),"> 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 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. Do you have any suggestions? Thank you for your help. Rose ==================================================================== Please note that this e-mail and any files transmitted from Memorial Sloan-Kettering Cancer Center may be privileged, confidential, and protected from disclosure under applicable law. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any reading, dissemination, distribution, copying, or other use of this communication or any of its attachments is strictly prohibited. If you have received this communication in error, please notify the sender immediately by replying to this message and deleting this message, any attachments, and all copies and backups from your computer. [[alternative HTML version deleted]]
On Feb 3, 2012, at 6:17 PM, brannona at 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 NAThat 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
Try this: apply(gc2, 2, function(x) ifelse(x < 0.1*median(x), NA, x)} ifelse is the vectorized operation you are probably looking for. Michael On Fri, Feb 3, 2012 at 6:17 PM, <brannona at 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: > >> 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)))," >> 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 > > > 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. > > Do you have any suggestions? > > > Thank you for your help. > Rose > > > ? ? ====================================================================> > ? ? Please note that this e-mail and any files transmitted from > ? ? Memorial Sloan-Kettering Cancer Center may be privileged, confidential, > ? ? and protected from disclosure under applicable law. If the reader of > ? ? this message is not the intended recipient, or an employee or agent > ? ? responsible for delivering this message to the intended recipient, > ? ? you are hereby notified that any reading, dissemination, distribution, > ? ? copying, or other use of this communication or any of its attachments > ? ? is strictly prohibited. ?If you have received this communication in > ? ? error, please notify the sender immediately by replying to this message > ? ? and deleting this message, any attachments, and all copies and backups > ? ? from your computer. > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.