Reputation: 117
test <- data.frame(name = c(1, 2, 3, 4, 3, 3), value = c(1222, 11122, 111, 122, 318431, 29424))
I am writing a function with tidyverse 1.3.0
in R version 3.6.2 that extracts duplicated ids. So for the above example, it would return 3. The following works as I desire:
getDupIds <- function(df, id_type) {
list <- df %>%
count_(id_type) %>%
filter(n > 1) %>%
pull(id_type)
list <- list[!is.na(list)]
if (length(list) != 0) {
return(list)
} else {
return("No duplicate ids.")
}
}
getDupIds(test, "name")
But I understand that underscored functions (count_
) are now to be replaced with unquoting. However, the following throws the error:
Error: Can't extract columns that don't exist. x The column `name` doesn't exist.
getDupIds <- function(df, id_type) {
list <- df %>%
count(!!id_type) %>%
filter(n > 1) %>%
pull(id_type)
list <- list[!is.na(list)]
if (length(list) != 0) {
return(list)
} else {
return("No duplicate ids.")
}
}
Any ideas why this doesn't work? It seems to unquote name
fine, it puzzles me why it cannot then find the column with that name.
Upvotes: 1
Views: 90
Reputation: 389155
Convert to symbol first before using !!
:
library(dplyr)
library(rlang)
getDupIds <- function(df, id_type) {
list <- df %>%
count(!!sym(id_type)) %>%
#You can also use
#count(.data[[id_type]]) %>%
filter(n > 1) %>%
pull(id_type)
list <- list[!is.na(list)]
if (length(list) != 0) return(list)
else return("No duplicate ids.")
}
getDupIds(test, "name")
#[1] 3
To pass unquoted column name you can use {{}}
, replace the part in function as :
list <- df %>%
count({{id_type}}) %>%
filter(n > 1) %>%
pull({{id_type}})
and call it like :
getDupIds(test, name)
Upvotes: 2