0

I have a dataframe in R as below. Is there a way to standardise the columns?

asd <- data.frame(a = c("test", 
                        "TEST", "new comment", "new Comment"), b = c(1,2, 3, 4), c = c("test", 
                        "TEST", "new comment", "new Comment"))

Expected out (All the words are capitalized in column a only with starting letter. Column c remains as is)

asd
            a b   c
1        Test 1  test
2        Test 2  TEST
3 New comment 3  new comment
4 New comment 4  new Comment
manu p
  • 952
  • 4
  • 10

1 Answers1

1

First, convert all letters to lowercase and next convert only the first letter to uppercase like this:

asd <- data.frame(a = c("test", 
                        "TEST", "new comment", "new Comment"), b = c(1,2, 3, 4), c = c("test", 
                                                                                       "TEST", "new comment", "new Comment"))

library(dplyr)
asd %>%
  mutate(a = tolower(a),
         a = paste0(toupper(substr(a, 1, 1)), substr(a, 2, nchar(a))))
#>             a b           c
#> 1        Test 1        test
#> 2        Test 2        TEST
#> 3 New comment 3 new comment
#> 4 New comment 4 new Comment

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

Quinten
  • 35,235
  • 5
  • 20
  • 53