select with one_of throws an error when one of the variable names does not exist

330 views
Skip to first unread message

Seth Wayland

unread,
Mar 30, 2015, 6:25:29 PM3/30/15
to manip...@googlegroups.com
I'm using one_of() with select() to select variables that match those from a character vector. When the character vector includes a name that is not one of the names in the tbl_df, then it throws an error.

It seems to me, from the function name "one_of" that the select should select all variables in the tbl_df that match the names passed into one_of() and ignore/drop ones that don't match. What is the intended action here? If passing variable names that don't exist in the tbl_df into one_of is supposed to fail, I think there could be a more informative message than "Error: Each argument must yield either positive or negative integers"

Here's a tiny example:
df <- data.frame(x = 1:10, y = runif(10))
df
%>% select(one_of("x","z"))

The error is actually coming from the call to combine_vars() because, in this case, one_of("x", "z") returns c(1L, NA) and dplyr:::combine_vars(c("x","y"), list(c(1L, NA_integer_))) throws the same error message.

I would be happy to add this as an issue on the dplyr github, but I wanted to see if I was missing something beforehand.
Opinions on what the default behavior of select(one_of()) should be?
Reply all
Reply to author
Forward
0 new messages