as.numeric function bug problem

Hi everyone,
I define discount rate variable as numeric. When i use this code
"a$DiscountRate<-as.numeric(a$DiscountRate)", null values turn into 3692 value. There are images below. How can i solve this problem. I tried to define null variables as 0.

this function does not work.
a$DiscountRate[is.null(a$DiscountRate)]=0

I tried to convers nulls into na by using this:
a$DiscountRate<-a$DiscountRate %>% replace(.=="NULL", NA)
a$DiscountRate[is.na(a$DiscountRate)]=0

After i define all null as 0 with this last operation, when i use this function:
a$DiscountRate<-as.numeric(a$DiscountRate)

zero values turn again into values as 3692


Thank you for your time

Can you provide a reproducible example instead of a screenshot?

It seems that your data begins life as a factor, and so its dangerous to cast with as.numeric probably need to convert to a character representation first.

a<-list()

a$DiscountRate <- c("1",".5","3.3","NULL","5")

a$DiscountRate2 <- factor(a$DiscountRate)

a$DiscountRate3 <- as.numeric(a$DiscountRate2)

a$DiscountRate4 <- as.numeric(a$DiscountRate)
> a
$DiscountRate
[1] "1"    ".5"   "3.3"  "NULL" "5"   

$DiscountRate2
[1] 1    .5   3.3  NULL 5   
Levels: .5 1 3.3 5 NULL

$DiscountRate3
[1] 2 1 3 5 4

$DiscountRate4
[1] 1.0 0.5 3.3  NA 5.0

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.