In R, you can use the substr
function or array-like indexing to get the first character of a string.
In this example,
str
with a value of "Hello World"
.substr
function with arguments str, start = 1, stop = 1
to get the first character.str <- "Hello World"
firstChar <- substr(str, start = 1, stop = 1)
print(paste("First character:", firstChar))
[1] "First character: H"
In this example,
str
with a value of "apple"
.1
to get the first character.str <- "apple"
firstChar <- str[1]
print(paste("First character:", firstChar))
[1] "First character: a"
In this tutorial, we learned How to get First Character in String in R language with well detailed examples.