How can I fix this na_if() Error?

I'm not sure how to fix this na_if() error.

Code I'm Trying to Run

change variable types and recode

educ.cleaned <- educ %>%
select(-...2) %>%
slice(1:280) %>%
rename(country = "Country", perc.in.school = "...3",
female.in.school = "...4", male.in.school = "...5") %>%
na_if("") %>%
mutate(perc.in.school = 100 - as.numeric(perc.in.school)) %>%
mutate(female.in.school = 100 - as.numeric(female.in.school)) %>%
mutate(male.in.school = 100 - as.numeric(male.in.school))

Output
Error in na_if():
! Can't convert y to match type of x <tbl_df>.
Run rlang::last_error() to see where the error occurred.

rlang::last_error()
<error/vctrs_error_cast>
Error in na_if():
! Can't convert y to match type of x <tbl_df>.


Backtrace:

  1. ... %>% ...
  2. dplyr::na_if(., "..")
    Run rlang::last_trace() to see the full context.

rlang::last_trace()
<error/vctrs_error_cast>
Error in na_if():
! Can't convert y to match type of x <tbl_df>.


Backtrace:

  1. ├─... %>% ...
  2. ├─dplyr::mutate(., male.in.school = 100 - as.numeric(male.in.school))
  3. ├─dplyr::mutate(., female.in.school = 100 - as.numeric(female.in.school))
  4. ├─dplyr::mutate(., perc.in.school = 100 - as.numeric(perc.in.school))
  5. ├─dplyr::na_if(., "..")
  6. │ └─vctrs::vec_cast(x = y, to = x, x_arg = "y", to_arg = "x")
  7. └─vctrs (local) <fn>()
  8. └─vctrs::vec_default_cast(...)
  9. ├─base::withRestarts(...)
    
  10. │ └─base (local) withOneRestart(expr, restarts[[1L]])
    
  11. │   └─base (local) doWithOneRestart(return(expr), restart)
    
  12. └─vctrs::stop_incompatible_cast(...)
    
  13.   └─vctrs::stop_incompatible_type(...)
    
  14.     └─vctrs:::stop_incompatible(...)
    
  15.       └─vctrs:::stop_vctrs(...)
    
  16.         └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = vctrs_error_call(call))
    

Not sure, but I think na_if wants a vector rather than a data frame. Maybe try replace_na.

I tried replace_na but it seems not to work with the data because it is in strings. I tried used na.omit and the code seems to work now but I did get a warning message about "NA introduced by coercion".

Output
Error in replace_na():
! replace must be a list, not a string.
Run rlang::last_error() to see where the error occurred.

rlang::last_error()
<error/rlang_error>
Error in replace_na():
! replace must be a list, not a string.


Backtrace:

  1. ... %>% ...
  2. tidyr:::replace_na.data.frame(., "..")
    Run rlang::last_trace() to see the full context.

educ.cleaned <- educ %>%

  • select(-...2) %>%
  • slice(1:280) %>%
  • rename(country = "Country", perc.in.school = "...3",
  •      female.in.school = "...4", male.in.school = "...5") %>%
    
  • na.rm("..") %>%
  • mutate(perc.in.school = 100 - as.numeric(perc.in.school)) %>%
  • mutate(female.in.school = 100 - as.numeric(female.in.school)) %>%
  • mutate(male.in.school = 100 - as.numeric(male.in.school))
    Error in na.rm(., "..") : could not find function "na.rm"

educ.cleaned <- educ %>%

  • select(-...2) %>%
  • slice(1:280) %>%
  • rename(country = "Country", perc.in.school = "...3",
  •      female.in.school = "...4", male.in.school = "...5") %>%
    
  • na.omit("..") %>%
  • mutate(perc.in.school = 100 - as.numeric(perc.in.school)) %>%
  • mutate(female.in.school = 100 - as.numeric(female.in.school)) %>%
  • mutate(male.in.school = 100 - as.numeric(male.in.school))
    Warning messages:
    1: There was 1 warning in mutate().
    :information_source: In argument: perc.in.school = 100 - as.numeric(perc.in.school).
    Caused by warning:
    ! NAs introduced by coercion
    2: There was 1 warning in mutate().
    :information_source: In argument: female.in.school = 100 - as.numeric(female.in.school).
    Caused by warning:
    ! NAs introduced by coercion
    3: There was 1 warning in mutate().
    :information_source: In argument: male.in.school = 100 - as.numeric(male.in.school).
    Caused by warning:
    ! NAs introduced by coercion

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.