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

type_of() in select() #1490

Closed
bhive01 opened this issue Oct 30, 2015 · 1 comment
Closed

type_of() in select() #1490

bhive01 opened this issue Oct 30, 2015 · 1 comment

Comments

@bhive01
Copy link

bhive01 commented Oct 30, 2015

Working on a complex dataset with text fields and numeric data. I need to split my dataset into two, summarize and rejoin. I need to be able to quickly select out the numeric columns and one other column (for grouping, summarizing and joining later), and then negate this for the other types.

My suggestion is a type_of() selection function to be able to put in the type of data.
select(DF, which(sapply(DF, typeof) == "double"))

From: https://stat.ethz.ch/R-manual/R-devel/library/base/html/typeof.html
"logical", "integer", "double", "complex", "character", "raw" and "list", "NULL", "closure"

Note: this doesn't work :/

type_of <- function(vars, match, ignore.case = TRUE) {
  if (ignore.case) match <- tolower(match) 
  if (match == "numeric") {match <- c("integer", "double", "complex")} 
  possibletypes <- c("logical", "integer", "double", "complex", "character", "raw", "list", "NULL", "closure")

  stopifnot(is.string(match), !is.na(match), nchar(match) > 0, !match %in% possibletypes)

  which(sapply(., typeof) == match))  # won't work, see below
} 

#looking at the code in
# https://github.com/hadley/dplyr/blob/dd10ccd543a2bea99573e6622b46d50f657b0c1f/R/select-vars.R # https://github.com/hadley/dplyr/blob/dd10ccd543a2bea99573e6622b46d50f657b0c1f/R/select-utils.R
# https://github.com/hadley/dplyr/blob/master/src/select.cpp
# the sub-selection functions appear to only see column names of the columns as "vars".
```r
@hadley
Copy link
Member

hadley commented Oct 30, 2015

Duplicate of #497

@hadley hadley closed this as completed Oct 30, 2015
@lock lock bot locked as resolved and limited conversation to collaborators Jun 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants