What does ~ function(.x) actually mean?

I stumbled upon this line while learning about the rename function in "dplyr" package. I am wondering what is the meaning of ~ and .x I have tried googling it but I couldn't find an answer.

rename_with(iris, ~ tolower(gsub(".", "_", .x, fixed = TRUE)))

in the context of dplyr it roughly means; what follows is an anonymous function where .x is the argument of the function.

in keeping with modern improvements to R, its preferred to be explicit, and so a preffered way to write the same command might be

rename_with(iris, \(x)tolower(gsub(".", "_", x, fixed = TRUE)))
1 Like

This topic was automatically closed 42 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.