How to find the numbers of characters in a string in R
Overview
A string like in every other programming language is used to store text. A string is always written in single quotation marks ('') or double quotation marks(""). Examples are: 'Hello world', "Hello World", etc.
These texts can also be referred to as characters. For instance, the first character in the string "Hello world" is "H", the sixth character is a space " " and the last character of the string is d.
To check for the number of characters present in a string, we use the
nchar()function of R.
Syntax
nchar(string)
Parameter(s)
string: Where the parameter value is thestringvariable in which you wish to find the number of characters present in it.
Example code
let’s create a string and calculate the number of characters present in that string.
# creating a stringmystring <- "Hello World!"# using the nchar() functionnchar(mystring)
From the output of the code above, we can see that using the nchar() function, the string "Hello World" contains just 12 characters.