How will you convert a factor variable to numeric in R language ?
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answer ( 1 )
1) Converting a Factor which is character
V = c(“East”, “South”, “East”, “North”)
# Convert vector ‘V’ into a factor
drn <- factor(V)
# Converting a factor into a numeric vector
as.numeric(drn)
OUTPUT :
1 3 1 2
2) Converting a Factor that is Number
If the factor is numeric, first convert it to a character vector and then to numeric
# Creating a Factor
price <- factor(c(15, 25, 5, 62, 29))
# Converting Factor to numeric
as.numeric(as.character(soap_cost))
OUTPUT :
15 25 5 62 29
NOTE :
The Factors corresponding to these numbers are
2 3 1 5 4