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

Statistics Globe
Statistics Globe
73.1 هزار بار بازدید - 5 سال پیش - How to convert a data
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
5 سال پیش در تاریخ 1398/10/16 منتشر شده است.
73,123 بـار بازدید شده
... بیشتر