OneBite.Dev - Coding blog in a bite size

convert variable from string to int in R

Code snippet on how to convert variable from string to int in R

  x <- as.integer(as.character(x))

This code converts a variable stored as a string (character) into an integer in R. The function “as.character” converts the variable from its default format (if applicable) into a string, and then “as.integer” converts the variable from the string format to an integer. The resulting converted variable is then stored in “x”. This code is useful for when dealing with data that a user has entered in a text field but should be in integer form for use in a program.

r