[R] Efficient swapping
Anthoni, Peter (IMK)
peter.anthoni at kit.edu
Fri Jul 7 10:42:18 CEST 2017
how about gdata functions?
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)))
tmp.orig=tmp
library(gdata)
bigMap=mapLevels(x=list(factor(tmp[,"R1"]),factor(tmp[,"R2"])),combine = T,codes=FALSE)
mapLevels(tmp[,"R1"]) = bigMap
mapLevels(tmp[,"R2"]) = bigMap
dd=as.integer(tmp[,"R2"])-as.integer(tmp[,"R1"])
odd=dd%%2!=0
swap=tmp[odd,"R2"]
tmp[odd,"R2"]=tmp[odd,"R1"]
tmp[odd,"R1"]=swap
cbind(tmp,odd,tmp.orig)
cheers
Peter
> On 7. Jul 2017, at 00:54, 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.
>
> ______________________________________________
> 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