How to Convert DataFrame Column from Character to Numeric in R

Here are four ways to convert a data frame column from character to numeric in R:

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

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

When converting the data frame column from character to numeric, ensure that the character strings represent numeric values. If not, as.numeric() will return NA for non-numeric strings.

Visual representation of converting DataFrame Column to Numeric in R

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 as.numeric() with $ operator

Visual representation of as.numeric() with $ operator

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 3: Using as.numeric() with [] operator

You can use the [] operator, which is helpful if you want to refer to columns by name or index in a more dynamic way.

Visual representation of using as.numeric() with [] operator

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 mutate() function from the dplyr package

Visual representation mutate() function from the dplyr package

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.

Leave a Comment