This question already has answers here:
Convert factor to integer [duplicate]
(2 answers)
Loading data issues
(1 answer)
Closed 10 years ago.
i have a variable that contains numbers as below:
[1] 76.19047619 71.42857143 70.37037037 78.72340426 94.54545455 86.53846154 80.58936579 57.77777778
[9] 96.00000000 89.79591837 81.25000000 100.00000000 92.00000000 61.53846154 96.42857143 88.88888889
when i bring this in R, they come as factors instead of numbers with the following message:
1577 Levels: #DIV/0! 0.00000000 0.05122951 0.05288207 0.06146281 0.13917884 0.26200873 0.26666667 ... 97.46376812
how can coerce them into numeric, i have tried as.integers and as.numeric but when i use that variable in computation, the results dont change. they are the same as when it is factors.
Thanks,
Related
This question already has answers here:
Change the class from factor to numeric of many columns in a data frame
(16 answers)
How to convert a factor to integer\numeric without loss of information?
(12 answers)
Closed 2 years ago.
so I have a csv file with around 60 variables that I have imported in R and I'd like to convert the columns to datatypes like numeric, Posixt, and Boolean. In my previous files I needed to do so only for a limited set of columns so I simply used 4-5 times.
data$var1 <- as.numeric(as.character(data$var1)
data$var2 <- as.numeric(as.character(data$var2)
For now however, when I want to convert 60 columns I'd rather not write 60 lines of code again and again. Could someone help me come up with a more efficient way?
This question already has answers here:
How to work with large numbers in R?
(1 answer)
Preserving large numbers
(8 answers)
Exponentiate very large numbers in R
(3 answers)
Closed 4 years ago.
I'm struggling to convert some big numbers to numeric in R while using "as.numeric"; even when combining it with as.character.
I have a character class column whose values are all numeric. To be specfic, 23 numbers in each value.
While trying to covert the whole column to numeric, the values are changing, and even if I try to do it just with one value I have the same problem (in numeric or character format)
that's what I've being trying:
as.numeric(as.character(20161128000215091389073))
as.numeric(as.character("20161128000215091389073"))
as.numeric(20161128000215091389073)
or
as.numeric(as.character("20161226000750155832528"))
as.numeric(as.character(20161226000750155832528))
as.numeric(20161226000750155832528)
returns respectively:
20161128000215089938222
and
20161226000750157496020
I couldn't make it return the same number... any ideas?
This question already has answers here:
How to convert a factor to integer\numeric without loss of information?
(12 answers)
Closed 8 years ago.
I have a data frame and I need to convert 2 variables from factor to numerical variables. I have a
df$QTY.SHIPPED=as.numeric(df$QTY.SHIPPED)
df$PRE.TAX.TOTAL.=as.numeric(df$PRE.TAX.TOTAL.)
The quantity shipped converts well. Because it is already in integer format. Howerver, the PRE.TAX.TOTAL. does not convert well.
PRE.TAX.TOTAL.(Factor) PRE.TAX.TOTAL.(Numerical)
57.8 3856
210 2159
Does anybody have an idea why it is converting this way?
Thank you
convert to character first and then to numeric. Otherwise it will just be converting to the underlying integer that encodes the factor
> v<-factor(c("57.8","82.9"))
> as.numeric(v)
[1] 1 2
> as.numeric(as.character(v))
[1] 57.8 82.9
You actually could read the documentation. Typing ?factor in console produces
Warning
The interpretation of a factor depends on both the codes and the
"levels" attribute. Be careful only to compare factors with the same
set of levels (in the same order). In particular, as.numeric applied
to a factor is meaningless, and may happen by implicit coercion. To
transform a factor f to approximately its original numeric values,
as.numeric(levels(f))[f] is recommended and slightly more efficient
than as.numeric(as.character(f)).
Thus, the more proper way would probably be as.numeric(levels(f))[f]
This question already has answers here:
Controlling number of decimal digits in print output in R
(4 answers)
Closed 9 years ago.
I am trying to have sum of two values in R with 6 decimal places but it only returns with 5.
85.85+0.01302778
# [1] 85.86303
I tried
round(85.85+0.01302778,6)
but it does not work.
Actually sum(85.85,0.01302778) gives only 5 decimals and I did not find any scope of decimal places in ?sum.
Any suggestions
Try this to get 6 digits:
> options(digits=8)
> 85.85+0.01302778
[1] 85.863028
This question already has answers here:
How to convert a factor to integer\numeric without loss of information?
(12 answers)
Closed 10 years ago.
> X864291X8X74
[1] 8.0000000000 9.0000000000 10.0000000000 6.0000000000 8.0000000000
10 Levels: 0.0000000000 10.0000000000 12.0000000000 3.0000000000 4.0000000000 6.0000000000 ... NULL
> as.numeric(X864291X8X74)
[1] 8 9 2 6 8
what did I misunterstood? shouldn't be the result of as.numeric 8 9 10 6 8?
How do I get the correct result?
Your vector is a factor. This question has been asked quite a few times, ex: here, here, here. In order to convert a factor to numeric, you'll have to convert to character first. Try:
as.numeric(as.character(my_vec))
The documentation at ?factor states:
To transform a factor f to approximately its original numeric values,
as.numeric(levels(f))[f] is recommended and slightly more efficient
than as.numeric(as.character(f)).
So the following works as well:
as.numeric(levels(my_vec))[my_vec]