Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort within a row of a data frame with categorical variables?

I have this code:

test <- data.frame("ClaimType1" = "Derivative", "ClaimType2" = "Derivative","ClaimType3" = "Class", "ClaimType4" = "Class", "Time1" = c(2,5), "Time2" = c(8,4), "Time3" = c(1,3), "Time4" = c(10,9))
claim1 claim2 claim3 claim4 time1 time2 time3 time4
Derivative Derivative Class Class 2 8 1 10
Derivative Derivative Class Class 5 4 3 9

I'm looking sort the get it in the following output:

claim1 claim2 claim3 claim4 time1 time2 time3 time4
Class Derivative Derivative Class 1 2 8 10
Class Derivative Derivative Class 3 4 5 9

I'm trying to sort within a row, but I'm not sure how to link the claim and times together. I'm guessing a dictionary wouldn't work here since it's an array.

like image 369
rushi Avatar asked Nov 20 '25 12:11

rushi


1 Answers

This is definitely much easier with long data, so, at least in dplyr, one has to pivot_longer then pivot_wider back:

library(dplyr)
library(tidyr)

test %>% 
  pivot_longer(cols = everything(), names_to = c(".value","col"), names_pattern = "(ClaimType|Time)(.*)") %>% 
  mutate(group = cumsum(col == 1)) %>% 
  arrange(group, Time, .by_group = T) %>% 
  mutate(col = sequence(rle(group)$l)) %>% 
  pivot_wider(id_cols = group, names_from = col, values_from = c("ClaimType","Time"), names_sep = "") %>% 
  select(-group)

  ClaimType1 ClaimType2 ClaimType3 ClaimType4 Time1 Time2 Time3 Time4
  <chr>      <chr>      <chr>      <chr>      <dbl> <dbl> <dbl> <dbl>
1 Class      Derivative Derivative Class          1     2     8    10
2 Class      Derivative Derivative Class          3     4     5     9
like image 133
Maël Avatar answered Nov 23 '25 01:11

Maël