logo
down
shadow

dplyr::group_by_ with character string input of several variable names


dplyr::group_by_ with character string input of several variable names

By : user2948123
Date : November 14 2020, 04:48 PM
fixed the issue. Will look into that further No need for interp here, just use as.formula to convert the strings to formulas:
code :
dots = sapply(y, . %>% {as.formula(paste0('~', .))})
mtcars %>% group_by_(.dots = dots)
~list(c("cyl", "gear"))
dots1 = sapply(y, . %>% {interp(~var, var = .)})
mtcars %>% group_by_(.dots = y)


Share : facebook icon twitter icon
standard evaluation in dplyr: summarise a variable given as a character string

standard evaluation in dplyr: summarise a variable given as a character string


By : Donna Lea Smith
Date : March 29 2020, 07:55 AM
around this issue With the release of the rlang package and the 0.7.0 update to dplyr, this is now fairly simple.
When you want to use a character string (e.g. "v1") as a variable name, you just:
code :
my_var <- "Sepal.Length"
my_sym <- sym(my_var)
summarize(iris, Mean = mean(!!my_sym))
my_var <- "Sepal.Length"
summarize(iris, mean(!!sym(my_var)))
library(rlang)

key <- "v3"
val <- "v2"
drp <- "v1"

df <- data_frame(v1 = 1:5, v2 = 6:10, v3 = c(rep("A", 3), rep("B", 2)))

df %>% 
  # NOTE: we don't have to do anything to `drp`
  # since the matches() function expects a character string
  select(-matches(drp)) %>% 
  group_by(!!sym(key)) %>% 
  summarise(sum(!!sym(val), na.rm = TRUE))
my_var <- "Sepal.Length"
my_sym <- sym(my_var)
summarize(iris, Mean = mean({{ my_sym }}))
dplyr: whats the difference between group_by and group_by_ functions?

dplyr: whats the difference between group_by and group_by_ functions?


By : user3221679
Date : March 29 2020, 07:55 AM
it fixes the issue The dplyr Non-Standard Evaluation vignette helps here: http://cran.r-project.org/web/packages/dplyr/vignettes/nse.html
Note: the above link is now out of date, but the same information can be found on the github page for the package. https://github.com/tidyverse/dplyr/blob/34423af89703b0772d59edcd0f3485295b629ab0/vignettes/nse.Rmd
Unexpected behavior in dplyr::group_by_ and dplyr::summarise_

Unexpected behavior in dplyr::group_by_ and dplyr::summarise_


By : user3543938
Date : March 29 2020, 07:55 AM
wish of those help I think you've misunderstood how summarise() works - it doesn't do anything with ., and the fact that it works at all is just happy chance. Instead, try something like this:
code :
library(dplyr)
get_r_squared <- function(x, y) summary(lm(x ~ y))$r.squared
mtcars %>% 
  group_by(cyl) %>% 
  summarise(r_squared = get_r_squared(mpg, wt))
Replacing group_by_ with group_by when the argument is a string in dplyr

Replacing group_by_ with group_by when the argument is a string in dplyr


By : Imran
Date : March 29 2020, 07:55 AM
should help you out Is this within a function? Otherwise I think the !!as.name() part is unnecessary and I would stick with the group_by_at(group_var) suggestion by @aosmith for simplicity sake. Otherwise, I would set it up as so:
code :
examplr <- function(data, group_var){
  group_var <- as.name(group_var)

  data %>% 
    group_by(!!group_var) %>% 
    summarize(mean_mpg = mean(mpg))
}

examplr(data = mtcars,
        group_var = "cyl")
Create a character variable with values conditional on previous variables (both their names and their values) with dplyr

Create a character variable with values conditional on previous variables (both their names and their values) with dplyr


By : user3368014
Date : March 29 2020, 07:55 AM
should help you out I have a dataframe with ~ 100 variables and several thousand observations. Some of these observations are disqualified from further analysis because of the values in some of their variables. Instead of just deleting these disqualified observations I want to create a character variable that indicates if the observation has been disqualified and if so, because of which variables (one observation can be disqualified because of multiple variables).
code :
library(dplyr)
df %>%
   #Check for 99 in specific columns
   mutate(disqualify = apply(.[,c('AA_B','A_B_C','A_BB','B_C')], 1, function(x) ifelse(any(x==99), 
                                   paste0("Disqualified because of ", paste(names(x[!is.na(x) & x==99]), collapse = " and ")), 
                                   NA)))

  id AA_B A_B_C A_BB B_C D_AC                           disqualify
1  1    1     0   -1  99    1          Disqualified because of B_C
2  2    0     0   99  NA    1         Disqualified because of A_BB
3  3   NA     0    0   1    1                                 <NA>
4  4    1    -1    0  99    1          Disqualified because of B_C
5  5   -1     1   -1   0   -1                                 <NA>
6  6   99    NA   NA  99   -1 Disqualified because of AA_B and B_C

#Base R
df$disqualify <- apply(df[,c('AA_B','A_B_C','A_BB','B_C')], 1, function(x) ifelse(any(x==99), 
                                                             paste0("Disqualified because of ", paste(names(x[!is.na(x) & x==99]), collapse = " and ")), 
                                                             NA))
Related Posts Related Posts :
  • Re coding in R using complicated statement
  • accumulating functions and closures in R
  • How do you combine two columns into a new column in a dataframe made of two or more different csv files?
  • Twitter authentication fails
  • Summing Values of One Vector Conditional on Values of Another Vector
  • draw cube into 3D scatterplot in RGL
  • lme4 translate formula to code in 3-level model
  • How to draw single axis plot in R
  • Combine geom_tile() and facet_grid/facet_wrap and remove space between tiles (ggplot2)
  • Use snpStats with R version 3.0.1
  • Makefile gives strange error while compiling markdown file into .docx file
  • How to determine whether a points lies in an ellipse
  • Summarize data already grouped in r
  • Is the bigvis package for R not available for R version 3.0.1?
  • Operator overloading in R reference classes
  • How to enable user to switch between ggplot2 and gVis graphs in R Shiny?
  • Is there an easy way to separate categorical vs continuous variables into two dataset in R
  • Correct previous year by id within R
  • Installation of rdyncall package for R
  • ggplot2 plot that evaluates the percentage and mean of a third variable at intersecting points
  • Error Handling with Lapply
  • data.table - split multiple columns
  • How to compute the overall mean for several files in R?
  • R: Graph Plotting: Subscripts in the legend like LaTeX
  • Restructuring data in R
  • Distance of pointsfrom cluster centers after K means clustering
  • R incorrect value of date function
  • Package "Imports" not loading in R development package
  • r - run a user defined function several times by taking column elements as parameters
  • Create input$selection to subset data AND radiobuttons to choose plot type in Shiny
  • Generate crosstabulations from dataframe of categorical variables in survey
  • Restructure output of R summary function
  • New behavior in data.table? .N / something with `by` (calculate proportion)
  • search certain number vector in R
  • R version doesn't support quartz graphic device - RStudio won't plot
  • Referencing a function parameter in R
  • How to synchronize signals using a cross-correlation and FFT in R?
  • Plotting coefficients and corresponding confidence intervals
  • passing expressions to curve() within a function
  • More effective merging of matched column with duplicates in data.table
  • Easy way to export multiple data.frame to multiple Excel worksheets
  • R Foreach Iterator - Walkforward
  • Table format and output in R
  • Restructuring data and duplicating rows in R
  • use ggplot2 to plot two lines with ribbons
  • how to plot a graph on lattice with two different colors
  • How can I keep a date formatted in R using sqldf?
  • Generating simulation data based on a specified distribution
  • Joining list of data frames in R
  • Subset data in R
  • R: How to avoid 2 'for' loops in R in this function
  • + signs appearing in console in R
  • how to create a dataframe form a lists within a list in R
  • Best way to combine and keep columns
  • Using identify and attach in a function
  • Apply function to each submatrix
  • How to assign regular strings for quarterly and monthly observation labels to the row names of a data frame?
  • Adjust hexbin legend breaks
  • Different lowess curves in plot and qplot in R
  • Extract words only with R
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org