5 Easy Ways to Convert DataFrame Column to Numeric in R

There are several ways to convert a “data frame column to numeric” in R:

  1. Using “transform()” function along with “as.numeric()”
  2. Using the “lapply()” function along with “as.numeric()”
  3. Using “as.numeric()” with “$” operator
  4. Using “as.numeric()” with “[]” operator
  5. Using the “mutate()” function from the “dplyr” package

Method 1: Using the transform() function along with as.numeric()

To convert a data frame column to numeric in R, the main way is to use the “transform()” function along with the “as.numeric()” function.

Example

df <- data.frame(
  COL1 = c("1", "2", "3", "4"),
  COL2 = c("11", "19", "21", "46")
)

# Print the original data frame
print("Original data frame:")
print(df)

# Convert the columns to numeric using transform() and as.numeric()
df_numeric <- transform(df,
  COL1 = as.numeric(COL1),
  COL2 = as.numeric(COL2)
)

# Print the converted data frame
print("Converted data frame:")
print(df_numeric)
sapply(df_numeric, class)

Output

[1] "Original data frame:"
  COL1  COL2
1  1     11
2  2     19
3  3     21
4  4     46
[1] "Converted data frame:"
   COL1 COL2
1   1    11
2   2    19
3   3    21
4   4    46
  COL1      COL2
"numeric" "numeric"

Method 2: Using lapply() function with as.numeric()

The “lapply()” function in R is used to apply a function to each element of a list or vector. The as.numeric() function converts an object to a numeric type.

Example

df <- data.frame(col1 = c("19", "21", "11"), col2 = c(19, 21, "11"))
print(df)
sapply(df, class)

df$col1 <- as.numeric(as.character(df$col1))
print(df)
sapply(df, class)

Output

  col1         col2
1 19            19
2 21            21
3 11            11

 col1           col2
 "character"  "character"
  
  col1         col2
1  19           19
2  21           21
3  11           11

   col1          col2
"numeric"     "character"

You can see that the first column is converted from character to numeric.

Method 3: Using as.numeric() with $ operator

The “$ operator” in R extracts a component from an object. The as.numeric() function converts an object to a numeric type.

Example

df <- data.frame(
  COL1 = c("1", "2", "3", "4"),
  COL2 = c("11", "19", "21", "46")
)

# Print the original data frame
print("Original data frame:")
print(df)

df$COL1 <- as.numeric(df$COL1)

# Print the converted data frame
print("Converted data frame:")
print(df)
sapply(df, class)

Output

[1] "Original data frame:"
   COL1   COL2
1   1      11
2   2      19
3   3      21
4   4      46

[1] "Converted data frame:"
    COL1   COL2
1    1      11
2    2      19
3    3      21
4    4      46
 
   COL1       COL2
 "numeric"  "character"

Method 4: Using as.numeric() with [] operator

The “[]” operator in R extracts a subset of an object. The as.numeric() function converts an object to a numeric type.

Example

df <- data.frame(
  COL1 = c("1", "2", "3", "4"),
  COL2 = c("11", "19", "21", "46")
)

# Print the original data frame
print("Original data frame:")
print(df)

df[, "COL1"] <- as.numeric(df[, "COL1"])

# Print the converted data frame
print("Converted data frame:")
print(df)
sapply(df, class)

Output

[1] "Original data frame:"
   COL1   COL2
1   1      11
2   2      19
3   3      21
4   4      46

[1] "Converted data frame:"
    COL1   COL2
1    1      11
2    2      19
3    3      21
4    4      46
 
   COL1       COL2
 "numeric"  "character"

Method 5: Using mutate() function from the dplyr package

The “mutate()” function from the dplyr package in R adds new variables to a data frame.

Example

library(dplyr)

df <- data.frame(
  COL1 = c("1", "2", "3", "4"),
  COL2 = c("11", "19", "21", "46")
)

# Print the original data frame
print("Original data frame:")
print(df)

df <- df %>%
  mutate(COL1 = as.numeric(COL1))

# Print the converted data frame
print("Converted data frame:")
print(df)
sapply(df, class)

Output

[1] "Original data frame:"
   COL1   COL2
1   1      11
2   2      19
3   3      21
4   4      46

[1] "Converted data frame:"
    COL1   COL2
1    1      11
2    2      19
3    3      21
4    4      46
 
   COL1       COL2
 "numeric"  "character"

Remember to handle potential issues with factors when converting columns to numeric. For example, if a column is a factor, convert it to a character and then to a numeric.

data$COL1 <- as.numeric(as.character(data$COL1))

Choose the method that best fits your needs and coding style.

That’s it.

Leave a Comment