Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selecting vars with starts_with, ends_with, contains and matches return wrong result when given pattern does not exist #498

Closed
leondutoit opened this issue Jul 15, 2014 · 3 comments
Assignees
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@leondutoit
Copy link
Contributor

Here is the behaviour:

> d <- tbl_df(data.frame(xxx = 1:2, yyy = 1:2, bxx = 1:2, bbb = 1:2))
> d %>% select(starts_with('nonsense'))
Source: local data frame [2 x 4]

  xxx yyy bxx bbb
1   1   1   1   1
2   2   2   2   2
> d %>% select(ends_with('nonsense'))
Source: local data frame [2 x 4]

  xxx yyy bxx bbb
1   1   1   1   1
2   2   2   2   2
> d %>% select(matches('nonsense'))
Source: local data frame [2 x 4]

  xxx yyy bxx bbb
1   1   1   1   1
2   2   2   2   2
> d %>% select(contains('nonsense'))
Source: local data frame [2 x 4]

  xxx yyy bxx bbb
1   1   1   1   1
2   2   2   2   2

Clearly the select function should not return all columns in the dataframe. It should either throw an error with a helpful message or return an empty dataframe. I am not sure which would be preferable.

From what I can see the problem is in the list of functions called select_funs in the select_vars_q function. One would have to catch the error in there and decide what to return accordingly. I would be happy to submit a pull request but don't really want to do work without hearing what you think the most appropriate return value would be :)

@hadley
Copy link
Member

hadley commented Jul 28, 2014

I think it should throw an error, something like "Failed to select any columns". It also needs to handle the case like select(mtcars, -(mpg:carb)).

@leondutoit
Copy link
Contributor Author

Cool. I'll send a pull request in a week or so. On holiday atm.

@hadley hadley added the bug label Aug 1, 2014
@hadley hadley self-assigned this Aug 1, 2014
@hadley hadley added this to the 0.3 milestone Aug 1, 2014
@hadley hadley closed this as completed in 8efdf6f Aug 12, 2014
@puterleat
Copy link

I'm not sure if this is a related issue, but the following seems inconsistent to me:

> data_frame(a=1, ba=1) %>%  select(starts_with("a"), ends_with("b")) %>% names
character(0)
> 
> data_frame(a=1, ab=1) %>%  select(starts_with("a"), ends_with("b")) %>% names
[1] "a"  "ab"

@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants