-3

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}
Joshua
  • 40,822
  • 8
  • 72
  • 132
michaszek
  • 3
  • 3
  • 2
    You should read some basic R documentation, because you don't seem to even know how to create a sequence of numbers (a "vector" in R-speak). – Spacedman Jun 13 '15 at 21:30
  • You may find [this tutorial on vectors in R](http://www.r-tutor.com/r-introduction/vector) and [this tutorial on functions in R](http://www.statmethods.net/management/userfunctions.html) useful. – josliber Jun 13 '15 at 21:31

1 Answers1

0

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
josliber
  • 43,891
  • 12
  • 98
  • 133