I have an example of what I want to do. For example, I have x = "5" and y = "-5". as.numeric(x)
gives me 5, but as.numeric(y)
gives me NA. How can I change y and make it -5?
Hi there,
By surrounding your values in quotes you're making them strings. If you remove the quotes they're treated as numeric:
x <- 5
y <- -5
as.numeric(x) #should return 5
as.numeric(y) #should return -5
I would also recommend using the assignment operator, <-
, rather than =
to prevent some complications in your code down the line.
Hope this helps!
Even with numbers as quoted characters this works as expected for me, could you make a reproducible example of your issue?
x <- "5"
y <- "-5"
as.numeric(x)
#> [1] 5
as.numeric(y)
#> [1] -5
If you've never heard of a reprex before, you might want to start by reading this FAQ:
My guess: your dash is not the right dash. Use charToRaw
and see what comes out. It should look something like this:
y <- "-5"
as.numeric(y)
# [1] -5
charToRaw(y)
# [1] 2d 35
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.