To convert character to integer column in R, you can use as.integer() or as.numeric() function.

The following methods show how you can do it with syntax.

Method 1: Use as.intger() Function

as.integer(df$column_name)

Method 2: Use as.numeric() Function

as.numeric(df$column_name)

The following examples show how you can use these methods.

Let’s first create data frame and also check datatype of each column of data frame.

# Create data frame
df <- data.frame(Start_date=as.Date(c("2000-05-21","2000-05-22","2000-05-23","2000-05-24","2000-05-25","2000-05-26")),
                 Machine_name = c("Machine1","Machine2","Machine1","Machine3","Machine2","Machine3"),
                 Value = c("108","120","135","95","98","105"),Reading= c(110,97,91,89,80,85))
                 
# Print data frame
print(df)

# print datatype
print(str(df))

Output:

  Start_date Machine_name Value Reading
1 2000-05-21     Machine1   108     110
2 2000-05-22     Machine2   120      97
3 2000-05-23     Machine1   135      91
4 2000-05-24     Machine3    95      89
5 2000-05-25     Machine2    98      80
6 2000-05-26     Machine3   105      85

'data.frame':	6 obs. of  4 variables:
 $ Start_date  : Date, format: "2000-05-21" "2000-05-22" "2000-05-23" ...
 $ Machine_name: chr  "Machine1" "Machine2" "Machine1" "Machine3" ...
 $ Value       : chr  "108" "120" "135" "95" ...
 $ Reading     : num  110 97 91 89 80 85
NULL

The following examples shows how to convert character to integer column of data frame in R.

Convert Character to Integer Column of Data Frame Using as.integer() Function

Let’s use as.integer() function to convert datatype of column character to integer:

# Create data frame
df <- data.frame(Start_date=as.Date(c("2000-05-21","2000-05-22","2000-05-23","2000-05-24","2000-05-25","2000-05-26")),
                 Machine_name = c("Machine1","Machine2","Machine1","Machine3","Machine2","Machine3"),
                 Value = c("108","120","135","95","98","105"),Reading= c(110,97,91,89,80,85))

# Change the datatype from string to integer format
df$Value <- as.integer(df$Value)

# Print data frame
print(df)

# print datatype
print(class(df$Value))

Output:

  Start_date Machine_name Value Reading
1 2000-05-21     Machine1   108     110
2 2000-05-22     Machine2   120      97
3 2000-05-23     Machine1   135      91
4 2000-05-24     Machine3    95      89
5 2000-05-25     Machine2    98      80
6 2000-05-26     Machine3   105      85

[1] "integer"

As the above code we chnage data type of value column of data frame. The output shows data frame and also shows datatype of value column of data frame.

Change DataType from Character to Integer Column of data frame Using as.numeric() Function

Let’s see how we can change datatype character to integer using as.numeric() function in R:

# Create data frame
df <- data.frame(Start_date=as.Date(c("2000-05-21","2000-05-22","2000-05-23","2000-05-24","2000-05-25","2000-05-26")),
                 Machine_name = c("Machine1","Machine2","Machine1","Machine3","Machine2","Machine3"),
                 Value = c("108","120","135","95","98","105"),Reading= c(110,97,91,89,80,85))

# Change the datatype from string to integer format
df$Value <- as.numeric(df$Value)

# Print data frame
print(df)

# print datatype
print(class(df$Value))

Output:

  Start_date Machine_name Value Reading
1 2000-05-21     Machine1   108     110
2 2000-05-22     Machine2   120      97
3 2000-05-23     Machine1   135      91
4 2000-05-24     Machine3    95      89
5 2000-05-25     Machine2    98      80
6 2000-05-26     Machine3   105      85

[1] "numeric"

The output shows data frame and datatype of value column of data frame which converted to numeric.

Note that when you convert character data to integer or numeric that data should be number. You can’t convert alphabetical, special characters to integer or numeric.