The arrange()
function is used to reorder rows of a data frame according to one of the variables/columns.
Here we can order the rows of the data frame by date, so that the first row is the earliest (oldest) observation and the last row is the latest (most recent) observation.
chicago <- arrange(chicago, date)
We can now check the first few rows
head(select(chicago, date, pm25tmean2), 3)
## date pm25tmean2
## 1 1987-01-01 NA
## 2 1987-01-02 NA
## 3 1987-01-03 NA
and the last few rows.
tail(select(chicago, date, pm25tmean2), 3)
## date pm25tmean2
## 6938 2005-12-29 7.45000
## 6939 2005-12-30 15.05714
## 6940 2005-12-31 15.00000
Columns can be arranged in descending order too by useing the special desc()
operator.
chicago <- arrange(chicago, desc(date))
Looking at the first three and last three rows shows the dates in descending order.
head(select(chicago, date, pm25tmean2), 3)
## date pm25tmean2
## 1 2005-12-31 15.00000
## 2 2005-12-30 15.05714
## 3 2005-12-29 7.45000
tail(select(chicago, date, pm25tmean2), 3)
## date pm25tmean2
## 6938 1987-01-03 NA
## 6939 1987-01-02 NA
## 6940 1987-01-01 NA
How would you do this in base R without dplyr
?