0

I tried to switch my data using "as.numeric", "as.factor", etc.

This is for the latest version in R, and I've been trying to solve by searching on some website but I can't find the solution

r604c_fixed <- as.numeric(Test$r604c) #convert to numerical scale

I expect the output will be a numeric vector, as I typed in a previous function, but the result is still a vector of NA's.

OTStats
  • 1,820
  • 1
  • 13
  • 22
  • Could you please be more specific on your question? Try providing the input data that you want to convert. Read this if you want to know how to frame your question. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – FAlonso Sep 15 '19 at 05:48
  • It sounds like you're trying to coerce NA values to numeric, which would simply return NA. What values are you expecting? – Bill O'Brien Sep 16 '19 at 14:05

1 Answers1

0

If you just want to convert your NA to 0 you can use mutate_if(is.numeric, coalesce, 0). Like so:

library(tidyverse)
# Set up a dataframe with an NA value
has_an_na <- data.frame(c('John Doe','Peter Gynn','Jolie Hope'), c(21000, 23400, NA), as.Date(c('2010-11-1','2008-3-25','2007-3-14')))
# Convert NA values to 0 in numeric columns
na_converted_to_zero <- has_an_na %>%
  mutate_if(is.numeric, coalesce, 0)

Let me know how useful you found this answer.

Thomas Petersen
  • 992
  • 6
  • 5