ordering non-decreasing functions then adding them in R

100 views Asked by At

Need help with basic R function:

Firstly order non-decreasing sequence from 4 different sequences and then order those 4 sequences into one.

Im totally green in programing so please make it the simplest way possible.

Edit1: puting some input data as required

A={3,2,1,2}
B={6,7,5,8}
C={12,11,9,10}
D={65,43,76,13}

I would like it to first order each sequence, so

A={1,2,2,3}
B={5,6,7,8}
C={9,10,11,12}
D={13,43,65,76}

and then to merge it

ABCD={1,2,2,4,5,6,7,8,9,10,11,12,13,43,65,76}
1

There are 1 answers

0
josliber On BEST ANSWER

If you want a function that takes vectors A, B, C, and D and input and outputs a sorted version of them, you can try:

sortAll <- function(A, B, C, D) sort(c(A, B, C, D))

And then you can run it with something like:

A <- c(1,2,2,3)
B <- c(5,6,7,8)
C <- c(9,10,11,12)
D <- c(13,43,65,76)
sortAll(A, B, C, D)
# [1]  1  2  2  3  5  6  7  8  9 10 11 12 13 43 65 76

If you wanted to write a function that combined and sorted any number of inputs, you could try:

sortAll <- function(...) sort(unlist(list(...)))
sortAll(A, B, C, D)
# [1]  1  2  2  3  5  6  7  8  9 10 11 12 13 43 65 76
sortAll(A)
# [1] 1 2 2 3