sales <- sales %>% filter(! (is.na(sales[["user_submitted_review"]])))
total_purchased_mean <- mean(sales[["total_purchased"]], na.rm = T)
sales_up <- sales %>% mutate(updated_total_purchased = if_else(is.na(total_purchased), total_purchased_mean, total_purchased))
What I expected to happen: I expected this to replace the corresponding row with ‘na’ in the total_purchased column with the mean of the total_purchased
What actually happened:
Error : x `false` must be a double vector not an integer vector
I’m confused, should I cast the total_purchased_mean as an integer.
Hello! Hopefully I can clarify on this matter. This type of error is actually a subtle check by the
if_else() function. From what I can see in the code,
total_purchased is being interpreted as an integer, but
total_purchased_mean ends up having a double type.
if_else() tries to make sure that everything in the column it creates (ie
updated_total_purchased) is the same type. R distinguishes between doubles and integers, even though they’re both numbers to us.
I would check the types of both
total_purchased_mean and the
total_purchased column. Then, make sure they are the same type and try it out again with
if_else(). I hope this helps!
Thanks it helped, I have been able to resolve the issue.