How to Reverse the Order of a Dataframe in R

Reverse row order in a data frame

You could use rev to reverse the row names

df[rev(rownames(df)),]
# A B
# 3 5 6
# 2 3 4
# 1 1 2

If you want to correct the new reversed row names, you could write a little function

flip <- function(data) {
new <- data[rev(rownames(data)), ]
rownames(new) <- NULL
new
}
flip(df)
# A B
# 1 5 6
# 2 3 4
# 3 1 2

How to reverse the order of dataframe in a pattern in R

Another base R option using order + ave

df[with(df, order(Stage, ave(1:nrow(df), Stage, FUN = seq_along))), ]

which gives

  Stage        Score
5 W-1 75
6 W-1 1 Min 04 Sec
3 W-2 45
4 W-2 1 Min 34 Sec
1 W-3 25
2 W-3 2 Min 10 Sec

Data

> dput(df)
structure(list(Stage = c("W-3", "W-3", "W-2", "W-2", "W-1", "W-1"
), Score = c("25", "2 Min 10 Sec", "45", "1 Min 34 Sec", "75",
"1 Min 04 Sec")), row.names = c(NA, -6L), class = "data.frame")

How to reverse the order of values in one column in a dataframe grouped by another column?

You can try ave if you are with base R:

df <- within(df,B <- ave(B,A,FUN = function(x) sort(x,decreasing = TRUE)))

which gives

> df
A B
1 A 3
2 A 2
3 A 1
4 B 2
5 B 1
6 C 5
7 C 4
8 C 3
9 C 2
10 C 1

How to reverse rows of a data.frame or data.table in R

We can use apply row-wise and reverse the non-NA values in each row.

df[-1] <- t(apply(df[-1], 1, function(x) {x[!is.na(x)] <- rev(x[!is.na(x)]);x}))

How to reverse the order of observations in a data frame with individuals that are of different lengths

We can arrange by ID as well

library(dplyr)
df %>%
arrange(ID, desc(num))

Is there a simple way of reversing the dates in a dataframe in R?

For ordering rows, you can use this:

BTC <- BTC[order(BTC$date),]

To remove several columns at once, you can use the package dplyr:

library('dplyr')

BTC <- BTC %>% select(-high, -low, -unix, -symbol, -Volume.USDT, -tradecount)

If the column are ordered from high to tradecount, you can shorten it to:

BTC <- BTC %>% select(-(high:tradecount))

Reverse the order of the rows for every 'id' in data in R

Here's an dplyr example:

library(tidyverse)
data <- tibble(id = rep(letters[1:3], each=3),
date = rep(as.Date(c("2022-07-01", "2022-06-25", "2022-05-15")), 3))

data %>% arrange(id, date)

# A tibble: 9 x 2
id date
<chr> <date>
1 a 2022-05-15
2 a 2022-06-25
3 a 2022-07-01
4 b 2022-05-15
5 b 2022-06-25
6 b 2022-07-01
7 c 2022-05-15
8 c 2022-06-25
9 c 2022-07-01

Base solution without using dplyr

data[order(data$id, data$date),]

Selecting observations within a data frame and reversing their order

One option is to group_split by ID and do the arrange by looping over the list with map based on whether any of the values 'b', 'e', 'g' are %n% the 'ID'

library(dplyr)
library(purrr)
out <- dat %>%
group_split(ID) %>%
map_dfr(~ if(any(c('b', 'e', 'g') %in% first(.x$ID)))
.x %>%
arrange(desc(time)) else .x)

out %>%
filter(ID %in% c('a', 'b'))
# A tibble: 20 x 3
# ID time var1
# <fct> <int> <dbl>
# 1 a 1 -0.560
# 2 a 2 -0.230
# 3 a 3 1.56
# 4 a 4 0.0705
# 5 a 5 0.129
# 6 a 6 1.72
# 7 a 7 0.461
# 8 a 8 -1.27
# 9 a 9 -0.687
#10 a 10 -0.446
#11 b 10 -0.473
#12 b 9 0.701
#13 b 8 -1.97
#14 b 7 0.498
#15 b 6 1.79
#16 b 5 -0.556
#17 b 4 0.111
#18 b 3 0.401
#19 b 2 0.360
#20 b 1 1.22

Or we can make use of arrange in a hacky way i.e. change the time to negative based on the ID 'b', 'e', 'g' while the rest is positive

out1 <- dat %>%
arrange(ID, time * c(1, -1)[c(1 + (ID %in% c('b', 'e', 'g')))])

-checking

all.equal(out, out1, check.attributes = FALSE)
#[1] TRUE


Related Topics



Leave a reply



Submit