Convert Data Frame Column to Numeric in R | Example: Change Factor & Character Variable | as.numeric

Published: 06 January 2020
on channel: Statistics Globe
73,476
568

How to convert a data frame variable to numeric in the R programming language. More details: https://statisticsglobe.com/convert-d...
R code:

data <- data.frame(x1 = c(1, 5, 8, 2), # Create example data frame
x2 = c(3, 2, 5, 2),
x3 = c(2, 7, 1, 2))
data$x1 <- as.factor(data$x1) # First column is a factor
data$x2 <- as.character(data$x2) # Second column is a character
data$x3 <- as.integer(data$x3) # Third column is an integer

sapply(data, class) # Get classes of all columns

data$x1 <- as.numeric(as.character(data$x1)) # Convert one variable to numeric

sapply(data, class) # Get classes of all columns


On this page of the site you can watch the video online Convert Data Frame Column to Numeric in R | Example: Change Factor & Character Variable | as.numeric with a duration of hours minute second in good quality, which was uploaded by the user Statistics Globe 06 January 2020, share the link with friends and acquaintances, this video has already been watched 73,476 times on youtube and it was liked by 568 viewers. Enjoy your viewing!