简体   繁体   中英

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

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)

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)))
dat %>%
  group_by(vel, var) %>%
  mutate(across(everything(), sort)) %>%
  arrange(vel,var)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM