Home > Blockchain >  Swap values of two vectors at indexes that match a condition
Swap values of two vectors at indexes that match a condition

Time:08-03

Let's say I have two vectors of strings, and I want to swap the values so that at each index, the first vector will contain the string that sorts first. I could currently do:

a=month.name[1:6]
b=month.name[7:12]

swap=a>b
temp=a[swap]
a[swap]=b[swap]
b[swap]=temp

a # "January" "August" "March" "April" "May" "December"
b # "July" "February" "September" "October" "November" "June"

However is it possible to do something like the following instead?

swapwhen(a>b,a,b)

CodePudding user response:

we can use zeallot library , %<-% operator used to multiple assign values like found in other languages like python

library(zeallot)

c(a,b) %<-% list(ifelse(a > b , b , a ) ,
                 ifelse(a > b , a , b )) 
  • output
> a
[1] "January"  "August"   "March"    "April"    "May"     
[6] "December"

> b
[1] "July"      "February"  "September" "October"   "November" 
[6] "June"

CodePudding user response:

1) Use the pmin and pmax functions as shown:

a <- month.name[1:6]; b <- month.name[7:12]

pmin(a, b)
## [1] "January"  "August"   "March"    "April"    "May"      "December"

pmax(a, b)
## [1] "July"      "February"  "September" "October"   "November"  "June" 

1a) or this variation which also resets a and b to the new values:

a <- month.name[1:6]; b <- month.name[7:12]

list2env(list(a = pmin(a, b), b = pmax(a, b)), environment())

a
## [1] "January"  "August"   "March"    "April"    "May"      "December"
b
## [1] "July"      "February"  "September" "October"   "November"  "June"     

2) Also this may be of interest. It resets a and b to the new values.

library(gsubfn)
a <- month.name[1:6]; b <- month.name[7:12]

List(a, b) <- list(pmin(a, b), pmax(a, b))

a
## [1] "January"  "February" "March"    "April"    "May"      "June"    
b
## [1] "July"      "August"    "September" "October"   "November"  "December" 

CodePudding user response:

Actually I figured it out myself by modifying the code of this function (https://rdrr.io/cran/seqinr/src/R/swap.R):

swap <- function(x, y){
  x.sub <- substitute(x)
  y.sub <- substitute(y)
  x.val <- x
  e <- parent.frame()
  do.call("<-", list(x.sub, y.sub), envir = e)
  do.call("<-", list(y.sub, x.val), envir = e)
}

To:

swaw=function(w,x,y){
  x2=x;y2=y;y2[w]=x[w];x2[w]=y[w]
  e=parent.frame()
  do.call("=",list(substitute(x),x2),envir=e)
  do.call("=",list(substitute(y),y2),envir=e)
}

Then you get:

> a=month.name[1:6];b=month.name[7:12];swaw(a>b,a,b)
> a
[1] "January"  "August"   "March"    "April"    "May"      "December"
> b
[1] "July"      "February"  "September" "October"   "November"  "June"

CodePudding user response:

We can use ifelse

library(collapse)
c("a", "b") %=% list(ifelse(a > b, b, a), 
                  ifelse(a > b, a, b))
> a
[1] "January"  "August"   "March"    "April"    "May"      "December"
> b
[1] "July"      "February"  "September" "October"   "November"  "June"
  •  Tags:  
  • r
  • Related