[R] Efficient swapping

Ista Zahn istazahn at gmail.com
Fri Jul 7 01:43:00 CEST 2017


Untested, but I expect that setting the levels to be the same across the
two factors

levels(tmp$R1) <- levels(tmp$R2) <- LETTERS[1:6]

and proceeding as before should be fine.

Best,
Ista

On Jul 6, 2017 6:54 PM, "Gang Chen" <gangchen6 at gmail.com> wrote:

Thanks a lot, Ista! I really appreciate it.

How about a slightly different case as the following:

set.seed(1)
(tmp <- data.frame(x = 1:10, R1 = sample(LETTERS[1:5], 10, replace =
 TRUE), R2 = sample(LETTERS[2:6], 10, replace = TRUE)))

   x R1 R2
   1  C  B
   2  B  B
   3  C  E
   4  E  C
   5  E  B
   6  D  E
   7  E  E
   8  D  F
   9  C  D
  10  A  E

Notice that the factor levels between the two factors, R1 and R2,
slide by one level; that is, factor R1 does not have level F while
factor R2 does not have level A. I want to swap the factor levels
based on the combined levels of the two factors as shown below:

tl <- unique(c(levels(tmp$R1), levels(tmp$R2)))
for(ii in 1:dim(tmp)[1]) {
   kk <- which(tl %in% tmp[ii,'R2'], arr.ind = TRUE) - which(tl %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
  }
}

How to go about this case? Thanks!


On Thu, Jul 6, 2017 at 5:16 PM, Ista Zahn <istazahn at gmail.com> wrote:
> 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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list