On Aug 12, 2012, at 5:43 PM, Sachinthaka Abeywardana wrote:

Hi all,

It seems like I cannot use normal 'if' for data frames. What would be the
best way to do the following.

if data$col1='high'
   data$col2='H'
else if data$col1='Neutral'
   data$col2='N'
else if data$col='low'
  data$col2='L'
else
  #chuch a warning?


Note that col2 was not an existing column and was newly assigned for this
task.

Using arun's example, dat1:

 dat1$col2 <- c("H","N", "L","warn")[
match(dat1$col1, c("low", "Neutral", "high"), nomatch=4 )]

> dat1
      col1 col2
1     high    L
2     high    L
3     high    L
4  Neutral    N
5  Neutral    N
6  Neutral    N
7      low    H
8      low    H
9      low    H
10     low    H

Nested ifelse constructions would be quite inefficient. I'm not even sure that a 10-deep ifelse construction woul be acceptable to the interpreter. At one point I thought I read something about a nesting depth of 7 as being a limit. (But many years have passed since that reading and it's possible it's just a manufactured memory or that the limit has been raised.)

--

David Winsemius, MD
Alameda, CA, USA

______________________________________________
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