How do I change all the factor levels into lowercase and add their corresponding values

Alright, so I want to turn all the factor levels (they are letters eg. 'a', 'A', 'b', 'B') into lowercase and add their corresponding values (from another variable):

# SIMPLIFIED VERSION OF MY ORIGINAL CODE
letters_01 <- c("a", "A", "b", "B", "c", "C", "d", "D", "e", "E", "f", "F", "g", "G")
letters_02 <- c(1, 2, 1, 4, 5, 6, 3, 8, 9, 1, 11, 12, 13, 14)
letters.df.test <- data.frame(letters_01, letters_02)

When the uppercase levels become lowercase and therefore become the same level as they would now both be eg. a. (both would be lowercase) Okay take 'b' and 'B', when the uppercase version becomes lowercase I want it so there wouldn't be two lowercase 'b's, I also need to add the values for both 'b' and 'B' for example, 'b' = 4 and 'B' = 5 the new singular lowercase 'b' would equal 9.

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(stringr)) 
letters_01 <- c("a", "A", "b", "B", "c", "C", "d", "D", "e", "E", "f", "F", "g", "G")
letters_02 <- c(1, 2, 1, 4, 5, 6, 3, 8, 9, 1, 11, 12, 13, 14)
letters.df.test <- data.frame(letters_01, letters_02)
rm(letters_01,letters_02)
letters.df.test %>% mutate(letters_01 = str_to_lower(letters_01)) %>% group_by(letters_01) %>% summarise(sum(letters_02))
#> # A tibble: 7 x 2
#>   letters_01 `sum(letters_02)`
#>   <chr>                  <dbl>
#> 1 a                          3
#> 2 b                          5
#> 3 c                         11
#> 4 d                         11
#> 5 e                         10
#> 6 f                         23
#> 7 g                         27

Created on 2020-01-23 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.