[R] I know this is wrong, but why?

Stuart Luppescu s-luppescu at uchicago.edu
Fri Aug 2 22:57:25 CEST 2002


My mind has become corrupted by having to use SAS too much. I wanted to
calculate the difference of elements of two vectors if the signs are the
same. I tried it the corrupted way first:

if (effects1[,3] < 0 & effects0[,3] < 0) {
  imprv1 <- effects0[,3] - effects1[,3]
} else  if  (effects1[,3] > 0 & effects0[,3] > 0) {
  imprv1 <- effects1[,3] - effects0[,3]
} else {
  imprv1 <- NA

Then I realized it was giving me wrong results, (for one thing, I was
getting no NAs) and I should be doing it ``The R Way'' like this:

imprv1 <- rep(NA, 267)
imprv1[effects1[,3] < 0 & effects0[,3] < 0] <-  effects0[,3] - \
effects1[,3]
imprv1[effects1[,3] > 0 & effects0[,3] > 0] <-  effects1[,3] - \
effects0[,3]

But what I don't understand is why the first method doesn't work, or at
least why it didn't give me any warning or error messages. Can someone
enlighten me?

(I'm running R 1.5.1 on RedHat Linux 7.3 (intel).)
-- 
Stuart Luppescu -=- s-luppescu at uchicago.edu        
University of Chicago -=- CCSR 
才文と智奈美の父 -=-    Kernel 2.4.19-pre10-xf                
People don't usually make the same mistake twice --
 they make it three times, four time, five times...
 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 240 bytes
Desc: This is a digitally signed message part
Url : https://stat.ethz.ch/pipermail/r-help/attachments/20020802/fe2f8a7b/attachment.bin


More information about the R-help mailing list