The strtoi()
function in R is used to convert strings to integers according to the given base.
strtoi(x, base = OL)
The strtoi()
function takes the following parameter values:
x
: This represents the character vector.base
: This represents an integer between 2
and 36
inclusive, or zero (default) of which the string is to be converted to. This is an optional parameter.The strtoi()
function returns an integer vector of the same length as the character parameter value that is passed to it. It returns a NA
(not available) value for character strings that are not specified with a base value.
# creating string vectorsmystring1 <- c("A", "B")mystring2 <- c("1", "2")strtoi(mystring1)strtoi(mystring2)
mystring1
and mystring2
.strtoi()
function on both the variables mystring1
and mystring2
, without specifying a base.Note: Notice from the code output that the character string
mystring1
, which is not specified with abase
value in thestrtoi()
function returnedNA
.
# creating stringsmystring1 <- c("Hello", "World", "25C")mystring3 <- c("aaaa", "123", "BBB")strtoi(mystring1, 36L)strtoi(mystring3, 16L)
mystring1
and mystring3
.strtoi()
function on the two variables, using different parameter values for each of them.