Home > Mobile >  Change order value
Change order value

Time:05-14

I have a DB like this:

a <-c(4, 2, 10, 2, 10, 6, 2)
b <-c(4, 6, 70, 8, 18, 4, 3)
rbind(a,b)

I can create something like this:

x <- (4, 4, 2, 6, 10, 70, 2, 8, 10, 18, 6, 4, 3)

A DB that contains a and b order in this way, it is possible? I need to do a time series and the value must be in the same position

CodePudding user response:

rbind works only when both the vectors are of the same length or else it will give unexpected output. i.e.

> rbind(c(4, 3, 5), c(3, 4))
     [,1] [,2] [,3]
[1,]    4    3    5
[2,]    3    4    3
Warning message:
In rbind(c(4, 3, 5), c(3, 4)) :
  number of columns of result is not a multiple of vector length (arg 2)

and when we do c to make a vector, it returns an extra 3 from recycling


A more general approach is to order based on the sequence of values in both vector while concatenating the vectors`

c(a, b)[order(c(seq_along(a), seq_along(b)))]
[1]  4  4  2  6 10 70  2  8 10 18  6  4  2  3
  • Related