in Education by
When I convert a factor to a numeric or integer, I get the underlying level codes, not the values as numbers. f <- factor(sample(runif(5), 20, replace = TRUE)) ## [1] 0.0248644019011408 0.0248644019011408 0.179684827337041 ## [4] 0.0284090070053935 0.363644931698218 0.363644931698218 ## [7] 0.179684827337041 0.249704354675487 0.249704354675487 ## [10] 0.0248644019011408 0.249704354675487 0.0284090070053935 ## [13] 0.179684827337041 0.0248644019011408 0.179684827337041 ## [16] 0.363644931698218 0.249704354675487 0.363644931698218 ## [19] 0.179684827337041 0.0284090070053935 ## 5 Levels: 0.0248644019011408 0.0284090070053935 ... 0.363644931698218 as.numeric(f) ## [1] 1 1 3 2 5 5 3 4 4 1 4 2 3 1 3 5 4 5 3 2 as.integer(f) ## [1] 1 1 3 2 5 5 3 4 4 1 4 2 3 1 3 5 4 5 3 2 I have to resort to pasting to get the real values: as.numeric(paste(f)) ## [1] 0.02486440 0.02486440 0.17968483 0.02840901 0.36364493 0.36364493 ## [7] 0.17968483 0.24970435 0.24970435 0.02486440 0.24970435 0.02840901 ## [13] 0.17968483 0.02486440 0.17968483 0.36364493 0.24970435 0.36364493 ## [19] 0.17968483 0.02840901 Is there a better way to convert a factor to numeric? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
If you use as.numeric() directly on a factor, the result would be a vector of the internal level representations of the factor and not the original values. So, to convert a factor to a numeric with its original values intact, you need to either: Index the levels by the factor itself, and then to convert to numeric Use a nested function as.numeric(as.character(factor)) For example: f <- factor(sample(runif(5), 20, replace = TRUE)) num <- as.numeric(as.character(f)) num Output: [1] 0.4814881 0.4814881 0.8672543 0.1002661 0.8672543 0.1002661 0.3246189 0.4814881 [9] 0.7017931 0.1002661 0.3246189 0.8672543 0.3246189 0.3246189 0.8672543 0.8672543 [17] 0.8672543 0.3246189 0.4814881 0.3246189

Related questions

0 votes
    I have code that at one place ends up with a list of data frames which I really want to convert ... starting with (this is grossly simplified for illustration): listOfDataFrames...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have a data frame with a column of p-values and I want to make a selection on these p-values. > pvalues_anova ... 7.125751e-01 5.193604e-01 4.835312e-04 Selection way: anovatest...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have used the following ggplot command: ggplot(survey, aes(x = age)) + stat_bin(aes(n = nrow(h3), y = ... but cannot figure out how. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have a list and I want to remove a single element from it. How can I do this? I've tried looking ... 't found anything appropriate. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have a huge vector which has a couple of NA values, and I'm trying to find the max value in that vector ( ... I can compute the max? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Many times I have seen the set.seed function in R, before starting the program. I know it's basically used for ... need to set this? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    How do you import a plain text file as a single character string in R? I think that this will probably ... is probably unstable too. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    The install.packages() function in R is the automatic unzipping utility that gets and install packages in R. ... accesses packages? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    A very newbish question, but say I have data like this: test_data...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Suppose I have a ggplot with more than one legend. mov...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Let's say that I have a date in R and it's formatted as follows. date 2012-02-01 2012-02-01 2012-02- ... generate the day by the date. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Hi this simple code (and all my scripts from this morning) has started giving me an off-center title in ggplot2 Ubuntu ... the above this morning to try and fix this.... dat...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I'm trying to keep the legend of one layer (smooth) and remove the legend of the other (point). I have tried ... = gear)) + theme_bw() Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    A categorical variable V1 in a data frame D1 can have values represented by the letters from A to Z. I want to create a ... B','N',T')) Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    According to the R language definition, the difference between & and && (correspondingly | and ||) is that the former ... OrElse in R? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
...