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 $B:MJ8$HCRF`H~$NIc(B -=- 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
On 2 Aug 2002, Stuart Luppescu wrote:> 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 ><snip>> 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?Because if() isn't vectorised. Eg> if (1:10<5) "a" else "b"[1] "a" Only the first element is used. I would have thought there was a good case for a warning here, and I remember seeing one in the past (though it may have been in S-PLUS 3.x in the pre-R past). -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Stuart Luppescu <s-luppescu at uchicago.edu> writes:> 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?The main thing is that R generally deals in entire vectors and that if-statements will only make one decision, never do one thing for some elements and something else for the rest. In if (effects1[,3] < 0 & effects0[,3] < 0) the condition is a vector. but only the first element is used, as in> X <- c(T,T,F) > if (X) X else !X[1] TRUE TRUE FALSE> X <- c(F,T,F) > if (X) X else !X[1] TRUE FALSE TRUE for by-element decisions consider ifelse(), or, as you did, indexing. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Others have already replied to this pointing out that if ... else ... is a control statement whereas ifelse(...) is the vectorized function. Just a minor follow-up point. The way I would do this calculation is as follows: imprv1 <- ifelse(effects0[, 3]*effects1[, 3] > 0, sign(effects0[, 3]) * (effects1[, 3] - effects0[, 3]), NA) I'm not suggesting that your way is any less efficient, but this way does illustrate vectorization a bit more explicitly as well as the "whole object" view in a way which maximally contrasts with the SAS "line by line" view. Bill Venables.> -----Original Message----- > From: Stuart Luppescu [mailto:s-luppescu at uchicago.edu] > Sent: Saturday, August 03, 2002 6:57 AM > To: R-Help List > Subject: [R] I know this is wrong, but why? > > 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 > $B:MJ8$HCRF`H~$NIc(B -=- Kernel 2.4.19-pre10-xf > People don't usually make the same mistake twice -- > they make it three times, four time, five times... > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._