Here are four ways to convert a data frame column from character to numeric in R:
- Using transform() function along with as.numeric()
- Using as.numeric() with $ operator
- Using as.numeric() with [] operator
- 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.
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
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.
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
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.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.