Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change order value

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading