[R] Efficient swapping

Ista Zahn istazahn at gmail.com
Thu Jul 6 23:16:23 CEST 2017


How about

foo <- with(list(r1 = tmp$R1,
                 r2 = tmp$R2,
                 swapme = (as.numeric(tmp$R1) - as.numeric(tmp$R2)) %% 2 != 0),
{
    tmp[swapme, "R1"] <- r2[swapme]
    tmp[swapme, "R2"] <- r1[swapme]
    tmp
})

Best,
Ista

On Thu, Jul 6, 2017 at 4:06 PM, Gang Chen <gangchen6 at gmail.com> wrote:
> Suppose that we have the following dataframe:
>
> set.seed(1)
> (tmp <- data.frame(x = 1:10, R1 = sample(LETTERS[1:5], 10, replace =
> TRUE), R2 = sample(LETTERS[1:5], 10, replace = TRUE)))
>
>     x R1 R2
> 1   1  B  B
> 2   2  B  A
> 3   3  C  D
> 4   4  E  B
> 5   5  B  D
> 6   6  E  C
> 7   7  E  D
> 8   8  D  E
> 9   9  D  B
> 10 10  A  D
>
> I want to do the following: if the difference between the level index
> of factor R1 and that of factor R2 is an odd number, the levels of the
> two factors need to be switched between them, which can be performed
> through the following code:
>
> for(ii in 1:dim(tmp)[1]) {
>    kk <- which(levels(tmp$R2) %in% tmp[ii,'R2'], arr.ind = TRUE) -
> which(levels(tmp$R1) %in% tmp[ii,'R1'], arr.ind = TRUE)
>    if(kk%%2!=0) { # swap the their levels between the two factors
>       qq <- tmp[ii,]$R1
>       tmp[ii,]$R1 <- tmp[ii,]$R2
>       tmp[ii,]$R2 <- qq
>   }
> }
>
> More concise and efficient way to do this?
>
> Thanks,
> Gang
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list