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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With