Integers are whole numbers. Adding a character string to a numeric vector converts all the elements in the vector to the character.
Convert String to Integer in R
To convert strings to integers in R, use the strtoi() function. The strtoi() is a built-in function that converts strings to integers. The strtoi() function accepts two arguments and returns the integers.
Syntax
strtoi(x, base=0L)
Parameters
x: It is a character vector.
base: It is an integer between 2 and 36 inclusive; default is 0. For the default base = 0L, the base is chosen from the string representing that element of x, so different elements can have different bases. The standard C rules for choosing the base are that octal constants (prefix 0 not followed by x or X) and hexadecimal constants (prefix 0x or 0X) are interpreted as base 8 and 16; all other strings are interpreted as base 10.
Example
dt <- c("a", "b", "c")
ct <- c("A", "B", "C")
bt <- c("11", "21", "19")
# Calling strtoi() function for conversion
strtoi(dt, 16L)
strtoi(ct, 32L)
strtoi(bt)
Output
[1] 10 11 12
[1] 10 11 12
[1] 11 21 19
Convert Hex Strings to Integers in R
To convert hex strings into integers, use the strtoi() function.
dt <- c("0xff", "023", "025")
ct <- c("0000", "FFFF")
bt <- c("260", "121")
# Calling strtoi() function for conversion
strtoi(dt)
strtoi(ct, 16L)
strtoi(bt, 8L)
Output
[1] 255 19 21
[1] 0 65535
[1] 176 81
Convert a Character to Numeric in R
To convert a character to numeric in R, use the as.numeric() function.
rv <- c("-0.1", " 2.7 ", "3")
as.numeric(rv)
Output
[1] -0.1 2.7 3.0
That is it for converting a string to integer in R.
See also

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.