## How to sort a vector according to a given sequence in R

order function in r
r sort dataframe by column name
difference between sort and order in r
sort a vector in r
r sort dataframe descending
r sort vector by names
r sort dataframe by vector
r sort matrix by column

I'm trying to organize a sequence of data according to a given sequence. For example, the given sequence I have is

```set.seed(1)
given_seq <- sample(rep(1:3,2))
```

The data and its associated sequence

```dat_seq <- rep(1:3,2)
dat_value <- rnorm(6)
```

And I want to organize the data according to the given sequence. That is, 1,2,3 serve as a function of labels of data. For example,

```dat_value
  1.5952808  0.3295078 -0.8204684  0.4874291  0.7383247  0.5757814

dat_seq
 1 2 3 1 2 3

given_seq
 2 3 3 1 1 2
```

Then I expect the second and fifth data values (with label 2) are placed at first or sixth places.

I can see that the organized sequence is not unique, but I'm not sure how to do this.

Here's another option:

```dat_value[match(rank(given_seq, ties = "random"), rank(dat_seq, ties = "random"))]
#   0.7383247  0.5757814 -0.8204684  1.5952808  0.4874291  0.3295078
```

First we convert the two sequences into ones that have no repetitive elements; e.g.,

```rank(given_seq, ties = "random")
#  3 5 6 1 2 4
```

That is, if two entries of `given_seq` are, say, (1,1), then they will randomly be converted into (1,2) or (2,1). The same is done with `dat_seq` and, consequently, we can match them and reorder `dat_value` accordingly. Thus, this kind of method would give you some randomization, which may or may not be something desirable in your application.

How to Sort Data in R, , use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order. How to sort a vector in ascending order. To sort a vector, you use the sort () function. For example, to sort Population in ascending order, try this: > sort (some.states\$Population)  365 579 2110 2212 2541 3100 3615 4931 8277  21198.

I would just make the labels unique and use the `names` attribute normally.

```names(dat_value) = make.unique(as.character(dat_seq))
dat_value[make.unique(as.character(given_seq))]
#         2          3        3.1          1        1.1        2.1
# 0.3295078 -0.8204684  0.5757814  1.5952808  0.4874291  0.7383247
```

You can always strip the names off later if the non-uniqueness doesn't work for your use case.

Sorting, How do you sort a vector in descending order in R? In R, we can use the help of the function order(). In R, we can easily sort a vector of continuous variable or factor variable. Arranging the data can be of ascending or descending order. Syntax: sort(x, decreasing = FALSE, na.last = TRUE): Argument: x: A vector containing continuous or factor variable; decreasing: Control for the order of the sort method. By default, decreasing is set to `FALSE`.

This also works, probably even faster, although it may be harder to understand

```dat_value[order(dat_seq)][order(order(given_seq))]
```

First, we re-order dat_value so that it's corresponding to the sequence `c(1,1,2,2,3,3)`. Then we go for the desired order, which would be `given_seq` if that was sequential. Fortunately, twice calling order just makes it sequential.

R Program to Sort a Vector, How do I change the order of rows in R? For sort.int the value is the sorted vector unless index.return is true, when the result is a list with components named x and ix containing the sorted numbers and the ordering index vector. In the latter case, if method == "quick" ties may be reversed in the ordering (unlike sort.list ) as quicksort is not stable.

Reorder Data Frame Rows in R, , as follow. If the data contain missing values, they will always come at the end. I'm trying to organize a sequence of data according to a given sequence. For example, the given sequence I have is. set.seed(1) given_seq <- sample(rep(1:3,2)) The data and its associated sequence. dat_seq <- rep(1:3,2) dat_value <- rnorm(6) And I want to organize the data according to the given sequence.

Sorting or Ordering Vectors, Sort (or order) a vector or factor (partially) into ascending or descending order. for sort an R object with a class or a numeric, complex, character or logical vector. the sort order for character vectors will depend on the collating sequence of the For each of the result values in a specified position, any values smaller than  sort() takes a third parameter that is used to specify the order in which elements are to be sorted. We can pass “greater()” function to sort in descending order. This function does comparison in a way that puts greater element before.

How to order data frame rows according to vector with specific order , Sorting Vectors​​ To sort a vector in R use the sort() function. See the following example. By default, R will sort the vector in ascending order. However, you can add the decreasing argument to the function, which will explicitly specify the sort order as in the example above. Instead of using the with () function, we can simply pass the order () function to our dataframe. We indicate that we want to sort by the column of index 1 by using the dataframe [,1] syntax, which causes R to return the levels (names) of that index 1 column.

• what would be the expected outcome for the new order of `dat_value`?
• This seems really weird with non-unique labels. If your labels are unique, you could easily use the `names` attribute, but without unique labels you may have to build your own method. When you "expect the second and fifth data values (with label 2) are placed at first or sixth places", do you expect the 2nd and 5th must correspond with 1st and 6th? Or is it okay to be 6th and 1st? Is it okay if the 2nd is repeated at both 1st and 6th the 5th is missing?
• `order(order(given_seq))` is really clever!