[R] Seeing negative numbers to zero

Daniel Nordlund djnordlund at verizon.net
Fri Aug 7 23:50:08 CEST 2009

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of DebbieMB
> Sent: Friday, August 07, 2009 1:29 PM
> To: r-help at r-project.org
> Subject: [R] Seeing negative numbers to zero
> Hi,
> I am also new to R and I have a related question.  I am trying to set
> negative values in a single column of a dataframe to zero and 
> I can't seem
> to do it.
> I have tried:
> KN1<-subset(KN,select=c(5)) 
> # Here I am selecting the column of the dataframe KN1 and 
> assigning it the
> name KN2 - this step works
> KN2<-ifelse(KN1<=0,0,KN1) 
> # Here I am trying to set negative numbers to zero and leave all other
> numbers the same - this doesn't work
> Any help would be appreciated.
> Thanks,
> Debbie


When you say it doesn't work, what does that mean? Do you get an error
message?  Are you left with negative numbers? What?  A minimal,
self-contained, reproducible example would help us help you.

> KN1 <- rnorm(100)
> KN2 <- ifelse(KN1 < 0, 0, KN1) 

Works fine for fine for me.


Daniel Nordlund
Bothell, WA USA

More information about the R-help mailing list