4

I'm trying to convert all data in the column to "First letter to upper case" The following code replaces all data with the first row,

simpleCap <- function(x) {
  s <- strsplit(x, " ")[[1]]
  paste(toupper(substring(s, 1,1)), substring(s, 2),
        sep="", collapse=" ")
}
allDestination$Categories <- simpleCap(allDestination$Categories)

Sample data

japan/okinawa/okinawa-other-islands
japan/hokkaido/hokkaido-north/furano-biei-tomamu
japan/hokkaido/hokkaido-north/asahikawa-sounkyo
japan/hokkaido/hokkaido-north/wakkanai-rishiri-rebun
japan/hokkaido/hokkaido-east/kushiro-akan-nemuro

The function code sample comes from First letter to upper case

How to make the function as "column compatible" instead of only replacing only a single value?

Hami
  • 335
  • 1
  • 7
  • 22

2 Answers2

5

Tidyverse Answer - There is a function specifically for this stringr::str_to_title

library(tidyverse)

data example:

data_1 <- 
  data.frame("japan/okinawa/okinawa-other-islands",
             "japan/hokkaido/hokkaido-north/furano-biei-tomamu",
             "japan/hokkaido/hokkaido-north/asahikawa-sounkyo",
             "japan/hokkaido/hokkaido-north/wakkanai-rishiri-rebun",
             "japan/hokkaido/hokkaido-east/kushiro-akan-nemuro",
             stringsAsFactors=FALSE)

function at work:

> str_to_title(data_1)

[1] "Japan/Okinawa/Okinawa-Other-Islands"                 
[2] "Japan/Hokkaido/Hokkaido-North/Furano-Biei-Tomamu"    
[3] "Japan/Hokkaido/Hokkaido-North/Asahikawa-Sounkyo"     
[4] "Japan/Hokkaido/Hokkaido-North/Wakkanai-Rishiri-Rebun"
[5] "Japan/Hokkaido/Hokkaido-East/Kushiro-Akan-Nemuro"  

https://stringr.tidyverse.org/reference/case.html

mtelesha
  • 2,079
  • 18
  • 16
4

You can use a negative look behind, to capitalize every letter which is preceded by a non letter or is at the beginning of the sentence ie (?:^|(?<=\\W))(.) or also you can think of it as to capitalize a letter which is not preceded by a letter ie (?<!\\w)(.)

gsub("(?<!\\w)(.)","\\U\\1",dat1$V1,perl = TRUE) 

[1] "Japan/Okinawa/Okinawa-Other-Islands"                 
[2] "Japan/Hokkaido/Hokkaido-North/Furano-Biei-Tomamu"    
[3] "Japan/Hokkaido/Hokkaido-North/Asahikawa-Sounkyo"     
[4] "Japan/Hokkaido/Hokkaido-North/Wakkanai-Rishiri-Rebun"
[5] "Japan/Hokkaido/Hokkaido-East/Kushiro-Akan-Nemuro"    

data:

 dat1=read.table(strip=T,text="japan/okinawa/okinawa-other-islands
       japan/hokkaido/hokkaido-north/furano-biei-tomamu
       japan/hokkaido/hokkaido-north/asahikawa-sounkyo
       japan/hokkaido/hokkaido-north/wakkanai-rishiri-rebun
       japan/hokkaido/hokkaido-east/kushiro-akan-nemuro")
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • using `gsub("(?<!\\w)(.)","\\U\\1", tolower(dat1$V1), perl = TRUE)` would be robust to entries like `JAPAN/HOKKAIDO/HOKKAIDO/NORTH/FURANO/BIEI/ROMARU`. – Cris May 27 '22 at 20:54