Home > Blockchain >  Merging Vectors; odd position and even
Merging Vectors; odd position and even

Time:10-24

Trying to take the odd positions from a vector and merge it with another vector in which I'm taking the even ones. (sorry if this is a bit wordy) This is what I've done so far

Numbers = c(10,20,30,40,50,60,70,80,90,100,110,120)
x3=c(10,20,30,40,50,60,70,80,90) 
MV <- function(x) { seq(1,length(x),2) } 
MV(x3) 
#[1] 1 3 5 7 9 
MV2 <- function(x) { seq(1,length(x),1) }
MV2(x3) 
MV3 <- function(x) { c(MV,MV2) }
MV3(x3,Numbers)

I got a result I don't understand. I feel like I'm missing something here.

I want the function to return the odd position from x3 and the even from Numbers.
The goal would be

#[1] 10 20 30 40 50 60 70 80 90 100 120

Sorry if confusing because Numbers and x3 are basically

CodePudding user response:

Here is another option:

fn <- function(x, y){
  l1 <- length(x)
  l2 <- length(y)
  if(l1>l2) length(y) <- l1
  else length(x) <- l2
  even <- !seq_along(x) %% 2
  x[even] <- y[even]
  c(na.omit(x))
}

fn(x3, Numbers)
 [1]  10  20  30  40  50  60  70  80  90 100 120

CodePudding user response:

List Numbers is 3 values longer than list X3, which means a zipper approach isn't going to work. Unless we introduce NAs, and then remove them. (In this case, O[seq(E)] pads extra NAs to the end of the Odd list). split does the work of finding odds and evens:

O<-split(x3, 1:2)$`1`
E<-split(Numbers, 1:2)$`2`
D<-do.call(rbind, Map(rbind, O[seq(E)], E))
D<-D[!is.na(D)]
D
# [1]  10  20  30  40  50  60  70  80  90 100 120

CodePudding user response:

Here is a way. There are probably simpler solutions but I think this one works on all cases.

Numbers <- c(10,20,30,40,50,60,70,80,90,100,110,120)
x3 <- c(10,20,30,40,50,60,70,80,90) 

MV1 <- function(x) x[c(TRUE, FALSE)]
MV2 <- function(x) x[c(FALSE, TRUE)]
MV3 <- function(x, y) {
  x <- MV1(x)
  y <- MV2(y)
  m1 <- length(x)
  m2 <- length(y)
  n <- min(m1, m2)
  z <- c(rbind(x[seq.int(n)], y[seq.int(n)]))
  if(m1 == n)
    c(z, y[(n   1):m2])
  else c(z, x[(n   1):m1])
}

MV1(x3)
#> [1] 10 30 50 70 90
MV2(Numbers)
#> [1]  20  40  60  80 100 120
MV3(x3, Numbers)
#>  [1]  10  20  30  40  50  60  70  80  90 100 120

Created on 2022-10-23 with reprex v2.0.2

CodePudding user response:

I guess you can try order like below to merge two vectors alternately

> a <- x3[c(TRUE, FALSE)]

> b <- Numbers[c(FALSE, TRUE)]

> c(a, b)[order(c(seq_along(a), seq_along(b)))]
 [1]  10  20  30  40  50  60  70  80  90 100 120

CodePudding user response:

Updated alternative:

library(data.table)

f <- function(o,e) {
  rbind(
    SJ(o)[seq(1,.N,2)][,i:=.I],
    SJ(e)[seq(2,.N,2)][,i:=.I]
  )[order(i),V1]
}

f(x3,Numbers)

Output:

 [1]  10  20  30  40  50  60  70  80  90 100 120

Orginal answer

If you want to do this using a function, you can do it this way:

f <- function(x, even=T) x[seq(1 even,length(x),2)]

Now get the odd from x3 and the even from Numbers, and sort

sort(c(f(x3, F),f(Numbers)))

Output:

 [1]  10  20  30  40  50  60  70  80  90 100 120
  •  Tags:  
  • r
  • Related