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

Описание к видео Convert Data Frame Column to Numeric in R | Example: Change Factor & Character Variable | as.numeric

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

Комментарии

Информация по комментариям в разработке