Home > front end >  Consider words with equal capital and lowercase letters
Consider words with equal capital and lowercase letters

Time:11-06

I would like to make an adjustment to the code below. Note that I am averaging for Fridays and Thursdays. However, it is showing average for Thursday twice, this is because I have Thursday in capital letters and thursday in small letters in my database. However, I would like to make some adjustments that consider Thursday and thursday to be the same thing.

library(dplyr)

Test <- structure(list(date1 = as.Date(c("2021-11-01","2021-11-01","2021-11-01","2021-11-01")),
                       date2 = as.Date(c("2021-10-22","2021-10-22","2021-10-28","2021-10-30")),
                       Week = c("Friday", "Friday", "Thursday", "thursday"),
                       Category = c("FDE", "FDE", "FDE", "FDE"),
                       time = c(4, 6, 6, 3)), class = "data.frame",row.names = c(NA, -4L))
    meanTest1 <- Test %>%
      group_by(Week,Category) %>%
      dplyr::summarize(mean(time))

> meanTest1
  Week     Category `mean(time)`
1 Friday   FDE                 5
2 thursday FDE                 3
3 Thursday FDE                 6

CodePudding user response:

If we need it to be title case, use toTitleCase from tools, or else convert to lower case (tolower) or upper case (toupper) for the 'Week' column and use that in group_by

library(dplyr)
Test %>%
    group_by(Week = tools::toTitleCase(Week), Category) %>% 
    summarise(time = mean(time, na.rm = TRUE), .groups = 'drop')

-output

# A tibble: 2 × 3
  Week     Category  time
  <chr>    <chr>    <dbl>
1 Friday   FDE        5  
2 Thursday FDE        4.5
  •  Tags:  
  • r
  • Related