nse

How to optimize case_when in a function?

北城以北 提交于 2021-01-29 18:44:45
问题 I would like to write a function that creates a binning variable based on some raw data. Specifically, I have a dateset with the age values for each respondent and I would like to write a function that classifies that person into an age group, where the age group is a parameter of that function. This is what I started with: data <- data.frame(age = 18:100) foo <- function(data, brackets = list(18:24, 25:34, 35:59)) { require(tidyverse) tmp <- data %>% drop_na(age) %>% mutate(age_bracket =

How can you use a polynomial function programmatically in mutate?

笑着哭i 提交于 2020-06-16 04:17:48
问题 I want to use mutate to give me predicted values based on pre-specified variables with a quadratic / polynomial function. I can easily do this with a linear formula like this: library(tidyverse) xvar <- "Sepal.Length" yvar <- "Sepal.Width" ##linear fit #what does formula text look like? formula = !!sym(yvar) ~ !!sym(xvar) formula models <- iris %>% nest(-Species) %>% mutate( # Perform loess (or lm) calculation on each group m = map(data, lm, formula = !!sym(yvar) ~ !!sym(xvar) ), # Retrieve

dplyr: NSE in joins (by)

牧云@^-^@ 提交于 2020-05-12 04:28:26
问题 I had a hard time to figure out how I could join two tables using dplyr::left_join with NSE. The problem was that I could not supply the right value to 'by'. I think I have found a solution for now, but it feels like I am doing it in an extra complicated way. So, if you know an easier/more elegant solution, please let me know :) That's what I am doing: # some data df <- dplyr::tibble(x=1:10, y=LETTERS[1:10], z=LETTERS[11:20]) # some function test_fun <- function(df,id){ id <- rlang::enquo(id)

NSE challenge: break out of deparse(substitute(…))

房东的猫 提交于 2020-01-25 07:47:18
问题 Let's define: f <- function(x) deparse(substitute(x)) The challenge: find <something> so that f(<something>) returns "abc" . Excluding, of course, f(abc) . With "tidy NSE", i.e. quasiquoting, this is very easy. However, according to the NSE references (1, 2, 3), it is impossible since substitute is a pure quoting (as opposed to quasiquoting) function. I wonder if there is anything obscure or undocumented (not that uncommon!) that allows to unquote in substitute , hence the challenge. 回答1:

Dplyr function to compute average, n, sd and standard error

偶尔善良 提交于 2020-01-23 17:18:27
问题 I find myself writing this bit of code all the time to produce standard errors for group means ( to then use for plotting confidence intervals). It would be nice to write my own function to do this in one line of code, though. I have read the nse vignette in dplyr on non-standard evaluation and this blog post as well. I get it somewhat , but I'm too much of a noob to figure this out on my own. Can anyone help out? Thanks. var1<-sample(c('red', 'green'), size=10, replace=T) var2<-rnorm(10,

Specifying multiple variables to group by via explicit argument with unquoted elements

最后都变了- 提交于 2020-01-16 16:48:35
问题 Based on the section regarding capturing multiple arguments in Programming with dplyr, I am trying to specify multiple variables to group by in dplyr::group_by without relying on ... but using an explicit list argument group_vars instead without needing to quote the list elements in arg group_vars Example data df <- tibble::tribble( ~a, ~b, ~c, "A", "a", 10, "A", "a", 20, "A", "b", 1000, "B", "a", 5, "B", "b", 1 ) Approach based on ... from Programming with dplyr # Approach 1 ----- my

How do I access/use the name of an R object in a function? (part 2)

女生的网名这么多〃 提交于 2020-01-16 09:59:12
问题 For context, this is a continuation for this question I want to create a function that create a number of values from a vector of academic credits. 1.5 academic credits equal one week of studies in my country, that means I can use a vector of academic credits per module to determine the expected credits on a certain date. For the sake of clarity, the vector looks like this: Ekonom<-c(15,15,15,15,7.5,7.5,15,7.5,7.5,15,15,15,30,0) This, however, presupposes that I couple the vector of academic