[R] R-help Digest, Vol 91, Issue 21

Peter Dalgaard pdalgd at gmail.com
Tue Sep 21 22:17:20 CEST 2010


On 09/21/2010 09:47 PM, Kurt_Helf at nps.gov wrote:
> All
>      Is there a script in R equivalent to the "if then" transforms one can
> perform in Systat?  For example, I want to create a "Treatment" column
> coded either 1 or 2 for twelve field sites in a large data set.  Ideally,
> I'd be able to tell R to code sites a-f as 1 and sites g-l as 2.
> Cheers
> Kurt

Well, er, sort of.

Actually, there are multiple techniques in R to achieve the same effects
as you'd get using if statements in other languages.

e.g.

f.new <- f
levels(f.new) <- rep(1:2, each=6)

or

f.new <-f
levels(f.new) <- list("1"=letters[1:6],"2"=letters[7:12])

or

f.new <- factor(rep(1:2, each=6))[f]

or

f.new <- (as.numeric(f) > 6) + 1

or

f.new <- factor(ifelse(as.numeric(f) <= 6), 1, 2))

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list