In R, you can use the `substr` function to get the first N characters from a string.
In this example,
str
with a string value of "Hello World"
.substr
function with substr(str, 1, 5)
to get the first 5 characters.str <- "Hello World"
first5Chars <- substr(str, 1, 5)
cat('First 5 characters of the string are: ', first5Chars, '\n')
First 5 characters of the string are: Hello
In this example,
str
with a string value of "apple"
.substr
function with substr(str, 1, 3)
to get the first 3 characters.str <- "apple"
first3Chars <- substr(str, 1, 3)
cat('First 3 characters of the string are: ', first3Chars, '\n')
First 3 characters of the string are: app
In this tutorial, we learned How to get First N Characters in String in R language with well detailed examples.