Skip to content
Prev 166844 / 398503 Next

If...while...or what else??

Generally extraction of subsets is better done by subset().

myDat <- subset(dataset, subset= (variable==i) | (variable==j) )  #, or

myDat <- dataset[which( (dataset$variable==i) | (dataset 
$variable==j) ), ]
#note the need to add the name of the dataframe in the second version.

Both of these avoid the difficult to debug problem that might arise  
from the sometimes
incomplete evaluation of logical expressions. See the example from  
this morning:

---------
z <- data.frame( x = c(5,6,5,NA,7,5,4,NA),
                 y = c(1,2,2,2,2,2,2,2) )

p <- (z$x <= 5) & (z$y == 1)
p
z[p, "p1"] <-5
z
# ok, this works fine     (no NA's)  I suppose b/c (z$x <= 5) only  
gets evaluated once
z <- z[,-3]

p <- (z$x <= 5) & (z$y == 2)   # creates NA
p
z[p, "p2"] <-5            #throws an error due to the NA's
z
# this failed
--------------

The two versions of p above give different results using simple  
extraction, but
the subset function deals with the two conditions in what seems to be  
a sensible manner.

Then, of course, there is the standard warning: avoid naming objects  
"data".

You might gain efficiency by selecting once, and then not repeating
the calls to "analysis" I * J times. As I understand the R efficiency  
strategies,
one generally tries to avoid unnecessary repetition of "setting up".

-- 
David Winsemius
On Jan 13, 2009, at 8:20 AM, Niccol? Bassani wrote:

            
Sorry,did not see that you used "if" or "while" in the example.
That effort at extraction might fail. If "variable" is a column within
dataset, it is not a full-fledged object.