Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the order of elements in vector in R

Tags:

r

vector

I have a vector with elements in a certain row, but now I want to change the order of these elements. How can I do this with only one line of code?

# 1.create queue

queue <- c("James", "Mary", "Steve", "Alex", "Patricia")
queue

# 2.move Patricia to be in front of Steve

???

I am a beginner at R so make your responses as simple and watered down as possible! Thanks!

like image 359
Molly Tracy Avatar asked Sep 09 '14 01:09

Molly Tracy


People also ask

How do you change the order of vectors in R?

sort() function in R is used to sort a vector. By default, it sorts a vector in increasing order. To sort in descending order, add a “decreasing” parameter to the sort function.

How do I change the order of a list in R?

Sorting or Ordering a list in R can be done by using lapply() function or using the order() function after converting to a vector using unlist(). In this article, I will explain how to order a list of elements by name or values in R and by applying ascending or descending order.

How do I change an element to a vector in R?

Replace the Elements of a Vector in R Programming – replace() Function. replace() function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values.

How do I order a set of numbers in R?

To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order.


2 Answers

My moveMe function (in my SOfun package) is perfect for this. Once the package is loaded, you can do what you want with:

library(SOfun)
queue <- c("James", "Mary", "Steve", "Alex", "Patricia")
moveMe(queue, "Patricia before Steve")
# [1] "James"    "Mary"     "Patricia" "Steve"    "Alex"  

You can also compound commands by separating them with semicolons:

moveMe(queue, "Patricia before Steve; James last")
# [1] "Mary"     "Patricia" "Steve"    "Alex"     "James" 

moveMe(queue, "Patricia before Steve; James last; Mary after Alex")
# [1] "Patricia" "Steve"    "Alex"     "Mary"     "James" 

Options for moving include: "first", "last", "before", and "after".

You can also move multiple values to a position by separating them by commas. For instance, to move "Patricia" and "Alex" before "Mary" (reordered in that order) and then move "Steve" to the start of the queue, you would use:

moveMe(queue, "Patricia, Alex before Mary; Steve first")
# [1] "Steve"    "James"    "Patricia" "Alex"     "Mary"  

You can install SOfun with:

library(devtools)
install_github("SOfun", "mrdwab")

For a single value, moved before another value, you could also take an approach like the following:

## Create a vector without "Patricia"
x <- setdiff(queue, "Patricia")
## Use `match` to find the point at which to insert "Patricia"
## Use `append` to insert "Patricia" at the relevant point
x <- append(x, values = "Patricia", after = match("Steve", x) - 1)
x
# [1] "James"    "Mary"     "Patricia" "Steve"    "Alex" 
like image 133
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 19 '22 06:09

A5C1D2H2I1M1N2O1R2T1


One way to accomplish this is:

queue <- queue[c(1,2,5,3,4)]

But that's manual and not very generalizable. Basically, you reorder the vector by saying how to reorder the current indexes.

If you want to sort the queue alphabetically (which does cause Patricia to be in front of Steve):

queue <- sort(queue)
like image 31
Will Beason Avatar answered Sep 19 '22 06:09

Will Beason