Variable not found

Hello! I am trying to run this code, but I keep on getting the error "Error in .fn(...) : object '.v2clrelig' not found".

data %>%
replace_with_na_all(condition = ~ v2elffelrbin < 0)

The variable can be found in my data and I am able to select for it. It doesn't seem to be a typo because I have been copying and pasting. If anyone has any suggestions, please let me know!

I can't be sure without seeing a sample of your data, more detail on what you're trying to accomplish, and the packages you've loaded, but maybe the options below will be helpful.

Are you trying to operate on just the v2elffelrbin column? If so, then maybe the following:

library(tidyverse)

data %>%
  mutate(v2elffelrbin = replace(v2elffelrbin, v2elffelrbin < 0, NA_real_))

Or with the naniar package (which is overkill if you only want to operate on a single column),

library(naniar)

data %>%
  replace_with_na_at(.vars = c("v2elffelrbin"),
                     condition = ~ .x < 0)

If you want to replace values that are less than zero in any column:

library(naniar)

data %>%
  replace_with_na_all(condition = ~ .x < 0)

I think what's going wrong with your code (assuming you're using replace_with_an_all from the naniar package) is that replace_with_na_all doesn't take a specific column name in the formula argument, but instead uses .x as a "pronoun" to represent each of the columns in the data frame.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.