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 all numeric columns #497

Closed
steromano opened this issue Jul 14, 2014 · 4 comments · Fixed by #1880
Closed

Selecting all numeric columns #497

steromano opened this issue Jul 14, 2014 · 4 comments · Fixed by #1880
Assignees
Labels
feature a feature request or enhancement
Milestone

Comments

@steromano
Copy link

What's the best way of doing it?
The following

iris %>% select(which(sapply(., is.numeric)))

works with the latest version of magrittr. However,

iris %>% mutate_each(funs(log), which(sapply(., is.numeric)))

throws Error in lapply(X = X, FUN = FUN, ...) : object '.' not found, even with the updated chain operator. Is this the expected behaviour?

In general, what is the best way to filter columns by some boolean condition in dplyr? Would it be worth having another selection function (in addition to starts_with, ends_with, etc.) acting directly on columns instead of column names? E.g. something along the lines of

iris %>% select(satisfies(is.numeric))
@steromano
Copy link
Author

Right, just noticed that

iris %>% mutate_each_q(funs(log), which(sapply(., is.numeric)))

does work, which makes sense.

@hadley
Copy link
Member

hadley commented Jul 28, 2014

There's no best way currently. I think it's going to be fairly hard to implement across backend types, but maybe I could add some generic methods for determining column types.

@hadley hadley self-assigned this Aug 1, 2014
@hadley hadley added this to the 0.3.1 milestone Aug 1, 2014
@hadley hadley modified the milestones: 0.3.1, 0.4 Nov 18, 2014
@hadley hadley modified the milestones: 0.6, 0.5 Oct 22, 2015
@jennybc
Copy link
Member

jennybc commented Dec 20, 2015

The purrr package now offers nice solutions to @steromano's original question, at least for data frames.

To keep all numeric columns:

iris %>% 
  purrr::keep(is.numeric) %>% 
  head(2)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1          5.1         3.5          1.4         0.2
#> 2          4.9         3.0          1.4         0.2

To mutate all numeric columns with a single function:

iris %>%
  purrr::map_if(is.numeric, log) %>%
  head(2)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1     1.629241    1.252763    0.3364722   -1.609438  setosa
#> 2     1.589235    1.098612    0.3364722   -1.609438  setosa

lionel- added a commit to lionel-/dplyr that referenced this issue Jun 2, 2016
This function is analogous to summarise_if() and mutate_if()

Closes tidyverse#497, closes tidyverse#1569
lionel- added a commit to lionel-/dplyr that referenced this issue Jun 2, 2016
This function is analogous to summarise_if() and mutate_if()

Closes tidyverse#497, closes tidyverse#1569
hadley pushed a commit that referenced this issue Jun 2, 2016
* Add select_if() to select based on predicate

This function is analogous to summarise_if() and mutate_if()

Closes #497, closes #1569

* Silently add grouping variables in select_if()
@Fredo-XVII
Copy link

Nice! select_if() worked perfectly. Thank you.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants