Home > OS >  How to sort multiple columns in R data frame from smallest to largest by group?
How to sort multiple columns in R data frame from smallest to largest by group?

Time:08-24

My data is set out similar to the example below where I have two columns as identifiers (vel and var) and six columns representing difference scores (diff_one, diff_two etc.) which I'd like to arrange from smallest to largest.

I know I can arrange one column by group as in the case below, but I'd like to arrange/sort all six difference columns from smallest to largest by the grouping variables vel and var. Is there a clean way to do this? Thanks.

set.seed(10)

dat <- data.frame(
  
  vel = rep(c("slo", "med", "fas"), each = 28),
  var = rep(paste0("var", 1:7), times = 12),
  diff_one = rnorm(84, 0.03, 0.08),
  diff_two = rnorm(84, 0.03, 0.08),
  diff_three = rnorm(84, 0.03, 0.08),
  diff_four = rnorm(84, 0.03, 0.08),
  diff_five = rnorm(84, 0.03, 0.08),
  diff_six = rnorm(84, 0.03, 0.08)
  
)

test <- dat %>%
  filter(vel == "slo") %>%
  group_by(vel, var) %>%
  arrange(diff_one, .by_group = TRUE)

CodePudding user response:

dat %>%
  group_by(vel, var) %>%
  mutate(across(everything(), sort)) %>%
  arrange(vel,var)

CodePudding user response:

You can try:

cols = dat[, -c(1:2)]

#between columns
dat[, dat %in% cols] <- data.frame(t(apply(cols, 1, sort))) |> setNames(names(cols))

#within columns
dat[, dat %in% cols] <- do.call(data.frame, lapply(cols, sort))

edit:

I missed the grouping part.

For grouped df in base R

dat[, dat %in% cols] <- do.call(rbind, lapply(split(dat, list(dat$vel, dat$var)), 
\(x) sapply(x, sort)))
  •  Tags:  
  • r
  • Related