How will you convert a factor variable to numeric in R language ?

Question

Give an example

in progress 0
Dhruv2301 55 years 1 Answer 763 views Great Grand Master 0

Answer ( 1 )

  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

Leave an answer

Browse
Browse