nse

dplyr: select columns by position in NSE

早过忘川 提交于 2019-12-01 15:37:53
I am trying to create a function that will select columns in a DF based on their position. I will always need the first column and then a subset of the DF. I have 1 object for each subset I need to select. So far I have tried the following: position <- "1,28:31" DF %>% select_(.dots = position) but I receive the following error: Error in parse(text = x) : :1:2: unexpected 'x' It would seem the problem is the comma separation in the column position. Is there a workaround? You can just use select with a numeric vector of indexes: positions <- c(1,28:31) DF %>% select(positions) Using mtcars and:

dplyr: select columns by position in NSE

无人久伴 提交于 2019-12-01 14:36:45
问题 I am trying to create a function that will select columns in a DF based on their position. I will always need the first column and then a subset of the DF. I have 1 object for each subset I need to select. So far I have tried the following: position <- "1,28:31" DF %>% select_(.dots = position) but I receive the following error: Error in parse(text = x) : :1:2: unexpected 'x' It would seem the problem is the comma separation in the column position. Is there a workaround? 回答1: You can just use

rlang::sym in anonymous functions

无人久伴 提交于 2019-11-30 22:22:40
问题 I recently notices that rlang::sym doesn't seem to work in anonymous functions and I don't understand why. Here an example, it's pretty clumsy and ugly but I think it illustrates the point require(tidyverse) data <- tibble(x1 = letters[1:3], x2 = letters[4:6], val = 1:3) get_it <- function(a, b){ data %>% mutate(y1 = !!rlang::sym(a)) %>% mutate(y2 = !!rlang::sym(b)) %>% select(y1, y2, val) } get_it("x1", "x2") This defines some toy data and a (horrible) function that essentially renames the

Rename in dplyr 0.7+ function

不问归期 提交于 2019-11-29 14:26:39
I am having difficulty renaming columns in a function using dplyr. I have already found helpful posts on nonstandard evaluation and the use of enquo (e.g., http://dplyr.tidyverse.org/articles/programming.html and Changing names of resulting variables in custom dplyr function ). The ultimate goal is to use the function to summarise each group, and then rename the columns into something more meaningful than the original variable names. Here is a simple example, which works except for the commented-out lines. library(tidyverse) myfunc <- function(df, groupvar, colvar, grouplab, collab) { groupvar

Evaluate different logical conditions from string for each row

与世无争的帅哥 提交于 2019-11-28 13:38:34
I have a data.frame like this: value condition 1 0.46 value > 0.5 2 0.96 value == 0.79 3 0.45 value <= 0.65 4 0.68 value == 0.88 5 0.57 value < 0.9 6 0.10 value > 0.01 7 0.90 value >= 0.6 8 0.25 value < 0.91 9 0.04 value > 0.2 structure(list(value = c(0.46, 0.96, 0.45, 0.68, 0.57, 0.1, 0.9, 0.25, 0.04), condition = c("value > 0.5", "value == 0.79", "value <= 0.65", "value == 0.88", "value < 0.9", "value > 0.01", "value >= 0.6", "value < 0.91", "value > 0.2")), class = "data.frame", row.names = c(NA, -9L)) I would like to evaluate the strings in the condition column for every row. So the result

Scale value inside of aes_string()

天大地大妈咪最大 提交于 2019-11-28 13:04:03
I want to scale my y-var by multiplying it by a number, say 10, in ggplot. The problem is this is in a Shiny app and the variable must be passed as a character string, i.e. input$variable . How can I multiply one of the variables in aes_string() the same way I would in aes() ? Here is an example of when it fails: library(ggplot2) ggplot(data = subset(mtcars, cyl == 4), aes_string(x = "wt", y = "mpg")) + geom_line(size = 1.5, color = "#00868B") + geom_line(data = subset(mtcars, cyl == 8), aes_string(x = "wt", y = "mpg" * 10)) Error in "mpg" * 10 : non-numeric argument to binary operator You can

How to pass a named vector to dplyr::select using quosures?

非 Y 不嫁゛ 提交于 2019-11-28 11:17:33
Using the old select_() function, I could pass a named vector into select and change position and column names at once: my_data <- data_frame(foo = 0:10, bar = 10:20, meh = 20:30) my_newnames <- c("newbar" = "bar", "newfoo" = "foo") move_stuff <- function(df, newnames) { select_(df, .dots = newnames) } move_stuff(my_data, newnames = my_newnames) ) # this is the desired output # A tibble: 4 x 2 newbar newfoo <int> <int> 1 10 0 2 11 1 3 12 2 4 13 3 I tried doing something similar using quosures and splicing--selecting columns works great, but the names of the vectors (and thus renaming columns

R: Using a string as an argument to mutate verb in dplyr

六眼飞鱼酱① 提交于 2019-11-27 23:11:59
I am building a shiny app which needs to allow users to define new variables for plotting. Specifically I want to allow users to define an expression to be used in mutate verb. The server receives the expression as text and I am wondering how to make mutate execute it in dplyr 0.7. I can make it work (partially) using mutate_ but it is deprecated now. It also defines the new column name as the entire expression rather than the new variable Here is a reproducible example: input_from_shiny <- "Petal.ratio = Petal.Length/Petal.Width" iris_mutated <- iris %>% mutate_(input_from_shiny) This gives

Scoping of variables in aes(…) inside a function in ggplot

大憨熊 提交于 2019-11-27 23:11:21
Consider this use of ggplot(...) inside a function. x <- seq(1,10,by=0.1) df <- data.frame(x,y1=x, y2=cos(2*x)/(1+x)) library(ggplot2) gg.fun <- function(){ i=2 plot(ggplot(df,aes(x=x,y=df[,i]))+geom_line()) } if(exists("i")) remove(i) gg.fun() # Error in `[.data.frame`(df, , i) : object 'i' not found i=3 gg.fun() # plots df[,3] vs. x It looks like ggplot does not recognize the variable i defined inside the function, but does recognize i if it is defined in the global environment. Why is that? Note that this gives the expected result. gg.new <- function(){ i=2 plot(ggplot(data.frame(x=df$x,y

Why is enquo + !! preferable to substitute + eval

送分小仙女□ 提交于 2019-11-27 19:43:26
In the following example, why should we favour using f1 over f2 ? Is it more efficient in some sense? For someone used to base R, it seems more natural to use the "substitute + eval" option. library(dplyr) d = data.frame(x = 1:5, y = rnorm(5)) # using enquo + !! f1 = function(mydata, myvar) { m = enquo(myvar) mydata %>% mutate(two_y = 2 * !!m) } # using substitute + eval f2 = function(mydata, myvar) { m = substitute(myvar) mydata %>% mutate(two_y = 2 * eval(m)) } all.equal(d %>% f1(y), d %>% f2(y)) # TRUE In other words, and beyond this particular example, my question is: can I get get away