I loaded a .csv and one of the values is classified as a string, even though it's all numbers. I'm having difficulty turning the values into numbers.
I tried using the to.numeric()
and the stroi()
functions, but kept getting error messages
I loaded a .csv and one of the values is classified as a string, even though it's all numbers. I'm having difficulty turning the values into numbers.
I tried using the to.numeric()
and the stroi()
functions, but kept getting error messages
Please show a bit of your data. An easy way to do that is to post the output of the dput() function. If your data have been loaded into a data frame named DF, run
dput(head(DF))
and post the output here. Put a line with three back ticks just before and after the output you post, like this
```
Output of dput() goes here
```
c("42.75", "N/A", "48.91", "26.92", "29.95", "28.30")
That's my output, and R reads it as a character. Whatever solution I can find, I'm given an error message.
Using as.numeric() works for me.
X <- c("42.75", "N/A", "48.91", "26.92", "29.95", "28.30")
Xnum <- as.numeric(X)
Warning message:
NAs introduced by coercion
Xnum
[1] 42.75 NA 48.91 26.92 29.95 28.30
class(Xnum)
[1] "numeric"
The warning is displayed because "N/A" cannot be converted to a number.
I chose to store the result in a new variable named Xnum but you could overwrite the original variable X with the result of as.numeric(X).
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.