nse

Evaluate different logical conditions from string for each row

大城市里の小女人 提交于 2019-11-27 07:50:16
问题 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

Scale value inside of aes_string()

雨燕双飞 提交于 2019-11-27 07:38:55
问题 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

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

杀马特。学长 韩版系。学妹 提交于 2019-11-27 06:11:59
问题 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

dplyr::select() with some variables that may not exist in the data frame?

旧城冷巷雨未停 提交于 2019-11-27 05:54:42
问题 I have a helper function (say foo() ) that will be run on various data frames that may or may not contain specified variables. Suppose I have library(dplyr) d1 <- data_frame(taxon=1,model=2,z=3) d2 <- data_frame(taxon=2,pss=4,z=3) The variables I want to select are vars <- intersect(names(data),c("taxon","model","z")) that is, I'd like foo(d1) to return the taxon , model , and z columns, while foo(d2) returns just taxon and z . If foo contains select(data,c(taxon,model,z)) then foo(d2) fails

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

 ̄綄美尐妖づ 提交于 2019-11-26 23:16:43
问题 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

dplyr: How to use group_by inside a function?

喜你入骨 提交于 2019-11-26 03:56:04
问题 I want to use use the dplyr::group_by function inside another function, but I do not know how to pass the arguments to this function. Can someone provide a working example? library(dplyr) data(iris) iris %.% group_by(Species) %.% summarise(n = n()) # ## Source: local data frame [3 x 2] ## Species n ## 1 virginica 50 ## 2 versicolor 50 ## 3 setosa 50 mytable0 <- function(x, ...) x %.% group_by(...) %.% summarise(n = n()) mytable0(iris, \"Species\") # OK ## Source: local data frame [3 x 2] ##