missing values and nan's not allowed if 'na.rm' is false

Hi everyone,
I am struggling with this code right now. There are missing values in my data (NA) and I don't know how to fix the code to accommodate the fact that there are missing values. Thank you in advance!

This is the code:
mydata %>%
mutate(outlier=ifelse(is_outlier(selfesteem),selfesteem,as.numeric(NA))) %>%
ggplot(.,aes(x="", y=selfesteem))+
geom_boxplot()+
theme_classic()+
geom_text(aes(label=outlier),na.rm = TRUE, hjust=-.3)+
xlab("Information")+ ylab("Selfesteem rating")+
labs(title="Average rating of selfesteem")

It is telling me that:
Error: Problem with mutate() column outlier.
:information_source: outlier = ifelse(is_outlier(ovcompt), ovcompt, as.numeric(NA)).
x missing values and NaN's not allowed if 'na.rm' is FALSE
Run rlang::last_error() to see where the error occurred.

I assume you want to plot the points that aren't outliers? Are you sure you shouldn't use the following at the start of your code fragment:

i.e., swap the result arguments around. Also, it's probably easier (and quicker) to use NA_real_ rather than as.numeric(NA).

Stephen

I think you might have found the solution. You might also consider using "if_else" rather than "ifelse". Both work, but "if_else" is more consistent within the tidyverse and usually faster. See Vectorised if — if_else • dplyr (tidyverse.org) for more information.

Stephen

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.