You probably have a working solution by now, from the other responses, but s ince you're new to R I'd like to point out a few things.

(1)
Using subset() to pull out just one column is overkill. Two simpler ways are

  KN1 <- KN[ , 5]    ## note the comma
  KN1 <- KN[[5]]

And learning how to do indexing with the brackets [] is important.

Easier still would be if column 5 has a name, say "c5", then do

  KN1 <- KN$c5

The dollar sign is used with lists also, and is very useful to be familiar with.

(2)
The "c" around 5, i.e. c(5), was unnecessary. You only need c() when you want to concatenate more than one value, as in c(5,7).

(3)
You asked to set the negatives in a single column in a dataframe to zero -- that suggests (to me) that you want them set to zero within the dataframe, as opposed to a new variable outside the dataframe. To replace within the dataframe (and pretending that the column name is in fact "c5"), then

  KN$c5 <- ifelse( KN$c5 < 0, 0, KN$c5 )

will do it.

-Don

At 1:29 PM -0700 8/7/09, DebbieMB wrote:
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


tonybreyal wrote:

 see ?ifelse

 you didn't specify what happens if a value is exactly zero in the dataset
 and so i've just bundled it in with the negative case:

 x <- rnorm(20, 0, 1)
 y<-ifelse(x<=0, 10, 5)

 HTH,
 Tony Breyal


 cmga20 wrote:

 Hi i am very new to R and I have been trying to change each individual
 piece of data in a data set to 10 if it is below 0 and 5 if it is above
 0. I know this sounds very easy but i am struggling!!




--
View this message in context: http://*www.*nabble.com/For-loop-for-distinguishing-negative-numbers-tp24499872p24870518.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.


--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062

______________________________________________
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