R Change Letters of All List Elements – Lowercase & Uppercase (2 Examples)
In this R programming post you’ll learn how to set the character strings in a list to lowercase or uppercase.
Creating Example Data
example_list <- list(LETTERS[21:15], # Example list in R c("LaLaLa", "lOlOlOOOO"), "XXX", "yyy") example_list # Display example list in RStudio # [[1]] # [1] "U" "T" "S" "R" "Q" "P" "O" # # [[2]] # [1] "LaLaLa" "lOlOlOOOO" # # [[3]] # [1] "XXX" # # [[4]] # [1] "yyy" |
example_list <- list(LETTERS[21:15], # Example list in R c("LaLaLa", "lOlOlOOOO"), "XXX", "yyy") example_list # Display example list in RStudio # [[1]] # [1] "U" "T" "S" "R" "Q" "P" "O" # # [[2]] # [1] "LaLaLa" "lOlOlOOOO" # # [[3]] # [1] "XXX" # # [[4]] # [1] "yyy"
Example 1: Set List Elements to Uppercase Using lapply() & toupper() Functions
lapply(example_list, # Convert to uppercase toupper) # [[1]] # [1] "U" "T" "S" "R" "Q" "P" "O" # # [[2]] # [1] "LALALA" "LOLOLOOOO" # # [[3]] # [1] "XXX" # # [[4]] # [1] "YYY" |
lapply(example_list, # Convert to uppercase toupper) # [[1]] # [1] "U" "T" "S" "R" "Q" "P" "O" # # [[2]] # [1] "LALALA" "LOLOLOOOO" # # [[3]] # [1] "XXX" # # [[4]] # [1] "YYY"
Example 2: Set List Elements to Uppercase Using tolower() & toupper() Functions
lapply(example_list, # Convert to lowercase tolower) # [[1]] # [1] "u" "t" "s" "r" "q" "p" "o" # # [[2]] # [1] "lalala" "lololoooo" # # [[3]] # [1] "xxx" # # [[4]] # [1] "yyy" |
lapply(example_list, # Convert to lowercase tolower) # [[1]] # [1] "u" "t" "s" "r" "q" "p" "o" # # [[2]] # [1] "lalala" "lololoooo" # # [[3]] # [1] "xxx" # # [[4]] # [1] "yyy"
Related Articles & Further Resources
In the following, you can find some further resources on topics such as vectors, character strings, and data conversion: