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]
Related
This question already has answers here:
Calculating the maximum of sub-vectors of a larger vector
(2 answers)
Closed 3 years ago.
I searched at the forums but I could not find an answer for this.
I am looking for a way to convert
c(1,3,4,2,7,12,6,8,15)
to
c(1,3,4,4,7,12,12,12,15) using no loops.
It can be defined as a vector of historical highs of another vector.
This is an example, my data length will be fairly long.
thanks a lot,
Emre
We can use cummax from base R
cummax(v1)
#[1] 1 3 4 4 7 12 12 12 15
data
v1 <- c(1,3,4,2,7,12,6,8,15)
This question already has answers here:
How to convert a factor to integer\numeric without loss of information?
(12 answers)
Closed 5 years ago.
speed<- c("15","20","30")
factor <- factor(speed, ordered= TRUE, levels = c("15","20","30"))
I'm trying to calculate mean, sum, min of the factor. How do I do it when I used as.numeric()? It's giving the sum as 6, min as 1.
The problem is that as.numeric is not doing what you are expecting.
as.numeric(factor)
[1] 1 2 3
The answers that you got are correct for these numbers. But what I think you wanted was:
as.numeric(as.character(factor))
[1] 15 20 30
This question already has answers here:
Evaluate expression given as a string
(8 answers)
Closed 5 years ago.
This seems likely to have a simple solution but I am having a hard time getting it. How do I convert this string to numeric?
> a <- "1:10"
Desired solution should be
> 1:10
[1] 1 2 3 4 5 6 7 8 9 10
I have tried as.numeric() (doesn't work), strsplit ":" and getting the end points 1 and 10 (can work but seems clumsy) but is there some simpler way? Thanks.
You can use eval() and parse()
eval(parse(text ="1:10"))
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:
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,