The base R function for this task is toupper(x)
CapStr <- function(y) {
c <- strsplit(y, " ")[[1]]
paste(toupper(substring(c, 1,1)), substring(c, 2),
sep="", collapse=" ")
}
name<-c("sepal length", "sepal width", "petal length", "petal width", "species")
sapply(name, CapStr)
## sepal length sepal width petal length petal width species
## "Sepal Length" "Sepal Width" "Petal Length" "Petal Width" "Species"
capitalize_str <- function(charcter_string){
sapply(charcter_string, CapStr)
}
strsplit {base} Split the Elements of a Character Vector x into substrings according to the matches(specified by second argument “split”) to substring split within them.
name
## [1] "sepal length" "sepal width" "petal length" "petal width"
## [5] "species"
strsplit(name, " ")
## [[1]]
## [1] "sepal" "length"
##
## [[2]]
## [1] "sepal" "width"
##
## [[3]]
## [1] "petal" "length"
##
## [[4]]
## [1] "petal" "width"
##
## [[5]]
## [1] "species"
strsplit(name, "e")
## [[1]]
## [1] "s" "pal l" "ngth"
##
## [[2]]
## [1] "s" "pal width"
##
## [[3]]
## [1] "p" "tal l" "ngth"
##
## [[4]]
## [1] "p" "tal width"
##
## [[5]]
## [1] "sp" "ci" "s"
strsplit(name, " ")[[1]]
## [1] "sepal" "length"
substr {base} R Documentation “Substrings of a Character Vector: Extract or replace substrings in a character vector.
Usage
substr(x, start, stop) substring(text, first, last = 1000000L)…"
c <- strsplit(name, " ")[[1]]
substring(c, 1,1)
## [1] "s" "l"
substring(c, 2)
## [1] "epal" "ength"
chartr {base} R Documentation:
“Character Translation and Casefolding: Translate characters in character vectors, in particular from upper to lower case or vice versa.
Usage toupper(x)
Arguments
x
a character vector, or an object that can be coerced to character by as.character."
toupper(substring(c, 1,1))
## [1] "S" "L"
paste {base} R Documentation: “Concatenate Strings: Concatenate vectors after converting to character.
Usage
paste (…, sep = " “, collapse = NULL)
Arguments
… one or more R objects, to be converted to character vectors. sep a character string to separate the terms. Not NA_character_. collapse
an optional character string to separate the results. Not NA_character"
paste(toupper(substring(c, 1,1)), substring(c, 2), sep="", collapse=" ")
## [1] "Sepal Length"
Let’s apply this function CapStr # C & S capital
sapply(name, CapStr)
## sepal length sepal width petal length petal width species
## "Sepal Length" "Sepal Width" "Petal Length" "Petal Width" "Species"
sapply("Capitalize the first letter in a word string in R", CapStr)
## Capitalize the first letter in a word string in R
## "Capitalize The First Letter In A Word String In R"
Examples of function capitalize_str:
capitalize_str(name)
## sepal length sepal width petal length petal width species
## "Sepal Length" "Sepal Width" "Petal Length" "Petal Width" "Species"
capitalize_str("Capitalize the first letter in a word string in R")
## Capitalize the first letter in a word string in R
## "Capitalize The First Letter In A Word String In R"
#install.packages("Hmisc")
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Loading required package: ggplot2
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, units
capitalize(name)
## [1] "Sepal length" "Sepal width" "Petal length" "Petal width"
## [5] "Species"