Re: [R] R code for if-then-do code blocks

2018-12-19 Thread Paul Miller via R-help
Hi Gabor, Richard, and Thierry, Thanks very much for your replies. Turns out I had already hit on Gabor's idea of "factor out" in writing an initial draft of the code converting from SAS to R. Below is the link Gabor sent describing this and other approaches. https://stackoverflow.com/questio

Re: [R] R code for if-then-do code blocks

2018-12-17 Thread Gabor Grothendieck
There is some discussion of approaches to this here: https://stackoverflow.com/questions/34096162/dplyr-mutate-replace-on-a-subset-of-rows/34096575#34096575 On Mon, Dec 17, 2018 at 10:30 AM Paul Miller via R-help wrote: > > Hello All, > > Season's greetings! > > Am trying to replicate some SAS

Re: [R] R code for if-then-do code blocks

2018-12-17 Thread Richard M. Heiberger
I got another 10% savings with this example by using only one subscripting adjustment. I also fixed a typo in my previous posting (which didn't affect the timing). microbenchmark( rmh={ d3 <-data.frame(ID=rownames(d1), d1, test1=0, test2=0,

Re: [R] R code for if-then-do code blocks

2018-12-17 Thread Richard M. Heiberger
this can be dome even faster, and I think more easily read, using only base R d1 <- data.frame(workshop=rep(1:2,4), gender=rep(c("f","m"),each=4)) ## needed by vector and rowbased, not needed by rmh library(tibble) library(plyr) library(magrittr) microbenchmark( vector = {d1 %>

Re: [R] R code for if-then-do code blocks

2018-12-17 Thread Thierry Onkelinx via R-help
Dear Paul, R's power is that is works vectorised. Unlike SAS which is rowbased. Using R in a SAS way will lead to very slow code. Your examples can be written vectorised d1 %>% rownames_to_column("ID") %>% mutate( test1 = ifelse(gender == "f" & workshop == 1, 7, 0), test2 = ifelse(ge

[R] R code for if-then-do code blocks

2018-12-17 Thread Paul Miller via R-help
Hello All, Season's greetings! Am trying to replicate some SAS code in R. The SAS code uses if-then-do code blocks. I've been trying to do likewise in R as that seems to be the most reliable way to get the same result. Below is some toy data and some code that does work. There are some thing