[R] Robust comparison coding
David Kane <David Kane
a296180 at mica.fmr.com
Thu May 24 21:22:02 CEST 2001
Are there guidelines in R for writing robust comparison codes? I am most
worried about situations in which the code must subset out some data without
knowing ahead of time the type of the variable that will be used for the
subseting. Here is a toy example:
> version
_
platform sparc-sun-solaris2.6
arch sparc
os solaris2.6
system sparc, solaris2.6
status
major 1
minor 2.3
year 2001
month 04
day 26
language R
> set.seed(9)
> temp <- data.frame(x = sample(11:13, 10, replace = TRUE))
> for(i in unique(temp[["x"]])) {cat("There are", sum(temp[["x"]] == i), "values equal to", i, "\n")}
There are 2 values equal to 13
There are 7 values equal to 11
There are 1 values equal to 12 # These are the answers that I want to see. But
# what if x is a factor?
> temp$x <- as.factor(temp$x)
> for(i in unique(temp[["x"]])) {cat("There are", sum(temp[["x"]] == i), "values equal to", i, "\n")}
There are 0 values equal to 3
There are 0 values equal to 1
There are 0 values equal to 2
Not what I want. The problem seems to be that R is using the underlying code
for the factor (1,2,3) instead of the level. (Note how the problem goes away
when you draw x from 1:3 instead of from 11:13.) Note that:
> unique(temp[["x"]])
[1] 13 11 12
Levels: 11 12 13
In any event, how can I write code so that the comparison works no matter what
mode (numeric, character, factor) x is? Or is the problem that a construction
like (i in unique(temp[["x"]])) is ill-advised for some reason?
Thanks,
Dave Kane
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list