[R] Seeing negative numbers to zero

Daniel Nordlund djnordlund at verizon.net
Sat Aug 8 00:19:20 CEST 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Daniel Nordlund
> Sent: Friday, August 07, 2009 2:50 PM
> To: r-help at r-project.org
> Subject: Re: [R] Seeing negative numbers to zero
> 
> > -----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
> 
> 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.

Mark Leeds kindly pointed out to me offline that Debbie's KN1 would be a
data frame and my solution doesn't work.  I don't know whether KN1 and KN2
need to be data frames or whether vectors would work.  If a vector is ok
then how about

> KN1 <- KN[[5]]
> KN2 <- ifelse(KN1 < 0, 0, KN1) 
> KN2

Hope this is helpful,

Dan
 
Daniel Nordlund
Bothell, WA USA




More information about the R-help mailing list