rlang

Modify certain values in a data frame by indirect reference to the columns

*爱你&永不变心* 提交于 2019-12-11 00:34:46
问题 I'm wrangling some data where we sort fails into bins and compute limited yields for each sort bin by lot. I have a meta table that describes the sort bins. The rows are arranged in ascending test order and some of the sort labels come in with non-syntactic names. sort_tbl <- tibble::tribble(~weight, ~label, 0, "fail A", 0, "fail B", 0, "fail C", 100, "pass") > sort_tbl # A tibble: 4 x 2 weight label <dbl> <chr> 1 0 fail A 2 0 fail B 3 0 fail C 4 100 pass I have a data table of limited yield

dplyr programming: unquote-splicing causes overscope error with complete() and nesting()

和自甴很熟 提交于 2019-12-11 00:33:47
问题 So I've started to dip my toes into the wonderful world of dplyr programming. I'm trying to write a function that accepts a data.frame, a target column, and any number of grouping columns (using bare names for all columns). The function will then bin the data based on the target column and count the number of entries in each bin. I want to keep a separate bin size for every combination of the grouping variables present in my original data.frame(), so I'm using the complete() and nesting()

Error using tidyeval quo() with gather()

我与影子孤独终老i 提交于 2019-12-10 22:54:38
问题 I am trying to run gather() on data frames, and programmatically assign the .key column name using !!quo(). But I keep getting 'Error: Invalid column specification'. I even found a closed ticket where it shows that it should work (https://github.com/tidyverse/tidyr/issues/293). I'm going to go back to using rename_() as a workaround, but it would be nice to use the more elegant NSE. library('tidyverse') data(mtcars) my_var <- 'my_col_name' The following works, but is a one-trick pony > mtcars

rlang: pass multiple groups with … to gather()

我与影子孤独终老i 提交于 2019-12-10 22:09:34
问题 Lets say I'd like to calculate the mean , min and max for an arbitraty amount of groups within a custom function. The toy data looks like this: library(tidyverse) df <- tibble( Gender = c("m", "f", "f", "m", "m", "f", "f", "f", "m", "f"), IQ = rnorm(10, 100, 15), Other = runif(10), Test = rnorm(10), group2 = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B") ) To achieve this for two groups (gender, group2) I could use df %>% gather(Variable, Value, -c(Gender, group2)) %>% group_by(Gender,

confusing behavior of purrr::pmap with rlang; “to quote” or not to quote argument that is the Q

安稳与你 提交于 2019-12-10 04:22:42
问题 I have a custom function where I am reading entered variables from a dataframe using rlang . This function works just fine irrespective of whether the arguments entered are quoted or unquoted. But, strangely enough, when this function is used with purrr::pmap , it works only if the argument is quoted. So I have two questions: Why does the function behavior this way? How can I make a function using rlang such that I won't have to quote the arguments even if used in purrr::pmap ? Here is a

Correct usage of dplyr::select in dplyr 0.7.0+, selecting columns using character vector

会有一股神秘感。 提交于 2019-12-09 10:01:47
问题 Suppose we have a character vector cols_to_select containing some columns we want to select from a dataframe df , e.g. df <- tibble::data_frame(a=1:3, b=1:3, c=1:3, d=1:3, e=1:3) cols_to_select <- c("b", "d") Suppose also we want to use dplyr::select because it's part of an operation that uses %>% so using select makes the code easy to read. There seem to be a number of ways which this can be achieved, but some are more robust than others. Please could you let me know which is the 'correct'

How to fix 'Quosures can only be unquoted within a quasiquotation context' error in R function

狂风中的少年 提交于 2019-12-08 02:58:11
问题 I am trying to write my first function using rlang and I am having some trouble fixing the following error. I've read the vignette, but didn't see a good example of what I'm trying to do. library(babynames) library(tidyverse) name_graph <- function(data, name, sex){ name <- enquo(name) sex <- enquo(sex) data %>% filter_(name == !!name, sex == !!sex) %>% select(year, prop) %>% ggplot()+ geom_line(mapping = aes(year, prop)) } name_graph(babynames, Robert, M) I'm expecting my distribution graph,

Tidyeval: pass list of columns as quosure to select()

烈酒焚心 提交于 2019-12-07 14:32:05
问题 I want to pass a bunch of columns to pmap() inside mutate() . Later, I want to select those same columns. At the moment, I'm passing a list of column names to pmap() as a quosure, which works fine, although I have no idea whether this is the "right" way to do it. But I can't figure out how to use the same quosure/list for select() . I've got almost no experience with tidyeval, I've only got this far by playing around. I imagine there must be a way to use the same thing both for pmap() and

Using quotations inside mutate: an alternative to mutate_(.dots = …)

…衆ロ難τιáo~ 提交于 2019-12-07 03:50:36
问题 I want to apply different functions to the same column in a tibble. These functions are stored in a character string. I used to do this with mutate_ and the .dots argument like this: library(dplyr) myfuns <- c(f1 = "a^2", f2 = "exp(a)", f3 = "sqrt(a)") tibble(a = 1:3) %>% mutate_(.dots = myfuns) This approach still works fine but mutate_ is deprecated. I tried to achieve the same result with mutate and the rlang package but did not get very far. In my real example myfuns contains about 200

Use dplyr coalesce in programming

℡╲_俬逩灬. 提交于 2019-12-07 02:50:49
问题 I'd like to use dplyr's programming magic, new to version 0.7.0, to coalesce two columns together. Below, I've listed out a few of my attempts. df <- data_frame(x = c(1, 2, NA), y = c(2, NA, 3)) # What I want to do: mutate(df, y = coalesce(x, y)) # Here's the expected output: #> # A tibble: 3 x 2 #> x y #> <dbl> <dbl> #> 1 1 1 #> 2 2 2 #> 3 NA 3 I thought fn1 would work, but it's treating varname as character on the right-hand side. fn1 <- function(varname) { mutate(df, UQ(varname) :=