Home > Mobile >  Removing and splitting and reordering of characters in R
Removing and splitting and reordering of characters in R

Time:07-10

I have a data frame with columns of these values. I want to split them into vectors and rearrange them according to Monday -> Sunday based on the values inside. Is there a way to do it? I tried using str_split (column_name,",") but it doesn't split the string.

c("[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Tuesday,Wednesday,Thursday,Friday]", 
"[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Tuesday,Wednesday,Thursday,Friday]", 
"[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Sunday,Thursday,Tuesday,Saturday]", 
"[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Tuesday,Wednesday,Thursday,Friday]", 
"[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Tuesday,Wednesday,Thursday,Friday]", 
"[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Tuesday,Wednesday,Thursday,Friday]", 
"[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Tuesday,Wednesday,Thursday,Friday]", 
"[Saturday,Monday,Thursday,Tuesday,Friday]")

CodePudding user response:

A base R solution -

You can create a vector of the order that you want. Remove [] from the string, split the data on comma, use order and match to get correct order, paste the string back.

If the vector shared is called x you can do -

correct_order <- c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 
                   'Friday', 'Saturday', 'Sunday')

sapply(strsplit(gsub('\\[|\\]', '', x), ','), function(y) {
  paste(y[order(match(y,correct_order))], collapse = ',')
})

# [1] "Monday,Tuesday,Wednesday,Thursday,Friday"
# [2] "Monday,Tuesday,Wednesday,Thursday,Friday"
# [3] "Monday,Tuesday,Wednesday,Thursday,Friday"
# [4] "Monday,Tuesday,Wednesday,Thursday,Friday"
# [5] "Monday,Tuesday,Wednesday,Thursday,Friday"
# [6] "Monday,Tuesday,Thursday,Saturday,Sunday" 
# [7] "Monday,Tuesday,Wednesday,Thursday,Friday"
# [8] "Monday,Tuesday,Wednesday,Thursday,Friday"
# [9] "Monday,Tuesday,Wednesday,Thursday,Friday"
#[10] "Monday,Tuesday,Wednesday,Thursday,Friday"
#[11] "Monday,Tuesday,Wednesday,Thursday,Friday"
#[12] "Monday,Tuesday,Wednesday,Thursday,Friday"
#[13] "Monday,Tuesday,Wednesday,Thursday,Friday"
#[14] "Monday,Tuesday,Wednesday,Thursday,Friday"
#[15] "Monday,Tuesday,Thursday,Friday,Saturday" 

If you need [] back -

result <- sapply(strsplit(gsub('\\[|\\]', '', x), ','), function(y) {
  paste(y[order(match(y,correct_order))], collapse = ',')
})

result <- sprintf('[%s]', result)
result

# [1] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
# [2] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
# [3] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
# [4] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
# [5] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
# [6] "[Monday,Tuesday,Thursday,Saturday,Sunday]" 
# [7] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
# [8] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
# [9] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
#[10] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
#[11] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
#[12] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
#[13] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
#[14] "[Monday,Tuesday,Wednesday,Thursday,Friday]"
#[15] "[Monday,Tuesday,Thursday,Friday,Saturday]" 

CodePudding user response:

Here is an alternative approach:

library(tidyverse)

as_tibble(x) %>% 
  mutate(row = row_number()) %>% 
  separate_rows("value") %>% 
  filter(value != "") %>% 
  mutate(value = fct_relevel(value, c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 
                                           'Friday', 'Saturday', 'Sunday')
                           )) %>% 
  group_by(row) %>% 
  arrange(value, .by_group = TRUE) %>% 
  summarise(new_col = toString(value)) %>% 
  select(-row)
    new_col                                     
   <chr>                                       
 1 Monday, Tuesday, Wednesday, Thursday, Friday
 2 Monday, Tuesday, Wednesday, Thursday, Friday
 3 Monday, Tuesday, Wednesday, Thursday, Friday
 4 Monday, Tuesday, Wednesday, Thursday, Friday
 5 Monday, Tuesday, Wednesday, Thursday, Friday
 6 Monday, Tuesday, Thursday, Saturday, Sunday 
 7 Monday, Tuesday, Wednesday, Thursday, Friday
 8 Monday, Tuesday, Wednesday, Thursday, Friday
 9 Monday, Tuesday, Wednesday, Thursday, Friday
10 Monday, Tuesday, Wednesday, Thursday, Friday
11 Monday, Tuesday, Wednesday, Thursday, Friday
12 Monday, Tuesday, Wednesday, Thursday, Friday
13 Monday, Tuesday, Wednesday, Thursday, Friday
14 Monday, Tuesday, Wednesday, Thursday, Friday
15 Monday, Tuesday, Thursday, Friday, Saturday 

data:

c("[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Tuesday,Wednesday,Thursday,Friday]", 
"[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Tuesday,Wednesday,Thursday,Friday]", 
"[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Sunday,Thursday,Tuesday,Saturday]", 
"[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Tuesday,Wednesday,Thursday,Friday]", 
"[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Tuesday,Wednesday,Thursday,Friday]", 
"[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Tuesday,Wednesday,Thursday,Friday]", 
"[Monday,Tuesday,Wednesday,Thursday,Friday]", "[Monday,Tuesday,Wednesday,Thursday,Friday]", 
"[Saturday,Monday,Thursday,Tuesday,Friday]")

CodePudding user response:

Another approach:

library(tidyverse)

days <-
  c(
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Monday,Sunday,Thursday,Tuesday,Saturday]",
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Monday,Tuesday,Wednesday,Thursday,Friday]",
    "[Saturday,Monday,Thursday,Tuesday,Friday]"
  )


dat <- tibble(days)

days_order <- c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 
                'Friday', 'Saturday', 'Sunday')

dat %>%
    mutate(
        days = str_remove_all(days, "\\[|\\]") %>%
            str_split(",") %>%
            map(.f = ~ paste0(sort(ordered(.x, days_order)), collapse = ",")) %>%
            flatten_chr()
    )

#> # A tibble: 15 × 1
#>    days                                    
#>    <chr>                                   
#>  1 Monday,Tuesday,Wednesday,Thursday,Friday
#>  2 Monday,Tuesday,Wednesday,Thursday,Friday
#>  3 Monday,Tuesday,Wednesday,Thursday,Friday
#>  4 Monday,Tuesday,Wednesday,Thursday,Friday
#>  5 Monday,Tuesday,Wednesday,Thursday,Friday
#>  6 Monday,Tuesday,Thursday,Saturday,Sunday 
#>  7 Monday,Tuesday,Wednesday,Thursday,Friday
#>  8 Monday,Tuesday,Wednesday,Thursday,Friday
#>  9 Monday,Tuesday,Wednesday,Thursday,Friday
#> 10 Monday,Tuesday,Wednesday,Thursday,Friday
#> 11 Monday,Tuesday,Wednesday,Thursday,Friday
#> 12 Monday,Tuesday,Wednesday,Thursday,Friday
#> 13 Monday,Tuesday,Wednesday,Thursday,Friday
#> 14 Monday,Tuesday,Wednesday,Thursday,Friday
#> 15 Monday,Tuesday,Thursday,Friday,Saturday

Created on 2022-07-09 by the reprex package (v2.0.1)

  • Related