0

I'm new to R and RStudio and I'm trying to learn how to use tools from the tidyverse package for the purposes of data analysis.

I'm currently using the mutate() function but am having trouble understanding why the symbol ~ is necessary to make the following code work?

mutate(Amphitrite_temps_clean, across(where(is.character), ~na_if(., "999.99")))

For some reason if I just remove the the '~' symbol (which I've learned is called a tilde) in front of the ~na_if function, the code stops working. It gives me the following error:

Error in mutate(Amphitrite_temps_clean, across(where(is.character), na_if(., :

Caused by error in vec_cast(): object '.' not found

Would someone be able to help explain to me what the tilde symbol does in this context? I've been struggling with finding answers online. Sorry for the naivety guys!

I've tried to search and see why the tilde function does but many of the answers seem to be referring to other uses of the ~ symbol in R; not necessarily in the context of dplyr - tidy verse.

M--
  • 25,431
  • 8
  • 61
  • 93

1 Answers1

0

In short, if you supply a function without any additional arguments, the function name (without parentheses) is sufficient. If you do want to pass additional arguments (such as na.rm = TRUE), you need to specify how these arguments should be passed to the function. To do so, you need to either define a function, specify an anonymous function, or use the tilde notation from purrr (now discouraged). More information here.

dufei
  • 2,166
  • 1
  • 7
  • 18