On Sep 21, 2010, at 3:47 PM, kurt_h...@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.

?ifelse  # probably the way most normally adjusted people would do it.

?match

> selection <- letters[1:15][sample( 1:15, 30, replace=TRUE)]
> selection
[1] "e" "k" "l" "b" "d" "e" "b" "b" "o" "o" "c" "n" "i" "f" "g" "k" "b" "f" "k"
[20] "e" "m" "d" "h" "e" "c" "o" "e" "h" "a" "i"

> match(selection, letters[1:7]) # over shot "f", guess I cannot count. [1] 5 NA NA 2 4 5 2 2 NA NA 3 NA NA 6 7 NA 2 6 NA 5 NA 4 NA 5 3
[26] NA  5 NA  1 NA
>  # can be used for logical indexing
[1] TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE FALSE [13] FALSE TRUE TRUE FALSE TRUE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
[25]  TRUE FALSE  TRUE FALSE  TRUE FALSE
> !is.na(match(selection, letters[8:15]) )
[1] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE [13] TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE
[25] FALSE  TRUE FALSE  TRUE FALSE  TRUE
> # testing to see if any fell through the cracks
> !is.na(match(selection, letters[8:15]) )| !is.na(match(selection, letters[1:7]) ) [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

Could do this for the case you requested:
Treatment <- 2 - !is.na(match(selection, letters[1:7]) )

TRUE is 1 and FALSE is zero in Boolean arithmetic (which R supports).

--
David.

Cheers
Kurt

***************************************************************
Kurt Lewis Helf, Ph.D.
Ecologist
EEO Counselor
National Park Service
Cumberland Piedmont Network
P.O. Box 8
Mammoth Cave, KY 42259
Ph: 270-758-2163
Lab: 270-758-2151
Fax: 270-758-2609
****************************************************************
Science, in constantly seeking real explanations, reveals the true majesty
of our world in all its complexity.
-Richard Dawkins

The scientific tradition is distinguished from the pre-scientific tradition in having two layers. Like the latter it passes on its theories but it also passes on a critical attitude towards them. The theories are passed on not as dogmas but rather with the challenge to discuss them and improve
upon them.
-Karl Popper

...consider yourself a guest in the home of other creatures as significant
as yourself.
-Wayside at Wilderness Threshold in McKittrick Canyon, Guadalupe Mountains
National Park, TX

Cumberland Piedmont Network (CUPN) Homepage:
http://tiny.cc/e7cdx

CUPN Forest Pest Monitoring Website:
http://bit.ly/9rhUZQ

CUPN Cave Cricket Monitoring Website:
http://tiny.cc/ntcql

CUPN Cave Aquatic Biota Monitoring Website:
http://tiny.cc/n2z1o

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

David Winsemius, MD
West Hartford, CT

______________________________________________
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