How to sort a data frame in R

How to sort a data frame in R, A vector can be sorted using the sort() function, but a data frame cannot. The behavior of the arrange() method, which sorts a data frame, is demonstrated below.

You must give the data frame and the column by which you want the data frame to be sorted in order to apply the arrange function on it.

The Role of Data Science in Preventing Fraud »

Additionally, you need to specify which way you want it to be ordered. Any ties will be broken if multiple sort conditions are specified.

Therefore, we declare that we want the column to be in decreasing or ascending order in order to sort our presidential data frame explained as an example.

How to sort a data frame in R

# A tibble: 6 x 4
  name       start      end        party     
  <chr>      <date>     <date>     <chr>     
1 Eisenhower 1953-01-20 1961-01-20 Republican
2 Kennedy    1961-01-20 1963-11-22 Democratic
3 Johnson    1963-11-22 1969-01-20 Democratic
4 Nixon      1969-01-20 1974-08-09 Republican
5 Ford       1974-08-09 1977-01-20 Republican
6 Carter     1977-01-20 1981-01-20 Democratic

Since there are only four presidential elections per cycle, several portions of this data set are incorrect.

Following the 1963 assassination of President Kennedy, Lyndon Johnson took control, and Gerald Ford succeeded President Nixon after his resignation in 1974.

As our data frame suggests, there were no presidential elections in 1962 or 1973. These values should be replaced with NAs, which is how R indicates missing values.

The ifelse() function can be used to accomplish this. Here, we replace the value of elected with NA if it is either 1962 or 1973. In the absence of that, we replace it with the value it now has.

Machine Learning Archives » Data Science Tutorials

In this instance, we can use the %in% operator to quickly determine whether the value of elected belongs to the vector consisting of 1962 or 1973 rather than testing to see whether it equals 1962 or 1973.

However we are not going into that, directly we can just utilize the arrange function,

presidential %>% arrange(desc(party),name)
# A tibble: 12 x 4
   name       start      end        party     
   <chr>      <date>     <date>     <chr>     
 1 Bush       1989-01-20 1993-01-20 Republican
 2 Bush       2001-01-20 2009-01-20 Republican
 3 Eisenhower 1953-01-20 1961-01-20 Republican
 4 Ford       1974-08-09 1977-01-20 Republican
 5 Nixon      1969-01-20 1974-08-09 Republican
 6 Reagan     1981-01-20 1989-01-20 Republican
 7 Trump      2017-01-20 2021-01-20 Republican
 8 Carter     1977-01-20 1981-01-20 Democratic
 9 Clinton    1993-01-20 2001-01-20 Democratic
10 Johnson    1963-11-22 1969-01-20 Democratic
11 Kennedy    1961-01-20 1963-11-22 Democratic
12 Obama      2009-01-20 2017-01-20 Democratic

Keep reading, and keep learning!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × five =