On 11-02-12 05:08, beky wrote:
There is a code from SPSS Syntax
do if sub(ATVK,2,2)="01".
comp strata=1.
else if sub(ATVK,2,2)>="05" and sub(ATVK,2,2)<="27".
comp strata=3.
else if sub(ATVK,4,2)>"20" or sub(ATVK,6,2)>"20".
comp strata=4.
Note that your first statement is overwritten by the one below.
else if sub(ATVK,4,2)>"00".
comp strata=2.
end if.
value labels strata 1 "R 2 "Li" 3 "M" 4 "La".
How can i rewrite it in R code?
# guessing your data
ATVK <- paste(
sample(LETTERS, 100, repl=T),
sample(sprintf("%02d", 0:30), 100, repl=T),
sample(sprintf("%02d", 0:30), 100, repl=T),
sample(sprintf("%02d", 0:30), 100, repl=T),
sample(letters, 100, repl=T),
sep=""
)
strata <- factor(rep(NA, 100), levels=c("R", "Li", "M", "La"))
# making the "as.nimeric(stubstr(...))" convenient
atsub2 <- as.numeric(substr(ATVK, 2, 3))
atsub4 <- as.numeric(substr(ATVK, 4, 5))
atsub6 <- as.numeric(substr(ATVK, 6, 7))
# didn't find a one-liner, however, this should work
strata[atsub2==1] <- "R"
strata[atsub2>=5 | atsub2<=27] <- "M"
# change statements to prevent overwriting
strata[atsub4>0] <- "Li"
strata[atsub4>20 | atsub6>20] <- "La"
summary(strata)
HTH, *S*
--
Sascha Vieweg, saschav...@gmail.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.