Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

string
character
r
communitycreator

How to find the numbers of characters in a string in R

Onyejiaku Theophilus Chidalu

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 the string variable 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 string
mystring <- "Hello World!"

# using the nchar() function 
nchar(mystring)
Calculating the number of characters using nchar() function.

From the output of the code above, we can see that using the nchar() function, the string "Hello World" contains just 12 characters.

RELATED TAGS

string
character
r
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring