Hello Team, I can run the function below: library(tidyverse) f2 <- function(indata, subgrp1){ ? indata0 <- indata ? temp? ? <- indata0 %>% select({{subgrp1}}) %>% arrange({{subgrp1}}) %>%? ? ? group_by({{subgrp1}}) %>% ? ? mutate(numbering =row_number(), max=max(numbering)) ? view(temp) ? f_table <- table(temp$Species) ? view(f_table) ? return(f_table) } f2(iris, Species) You can see the second argument I use Species only, and it works fine.? But If I say, I want the 2nd argument =?Petal.Width, Species , how should I write the argument? I did try?f2(iris, c(Petal.Width, Species)), but I got error message: Error: arrange() failed at implicit mutate() step.? * Problem with `mutate()` column `..1`. i `..1 = c(Petal.Width, Species)`. i `..1` must be size 150 or 1, not 300. I'm not sure how to fix the problem either in function or can fix it when using the function. Thank you, Kai [[alternative HTML version deleted]]
RStudio is **not** R. In particular, the so-called TidyVerse consists of all *non*-standard contributed packages, about which the Posting Guide (linked below) says: "For questions about functions in standard packages distributed with R (see the FAQ Add-on packages in R), ask questions on R-help. [The link is: https://cran.r-project.org/doc/FAQ/R-FAQ.html#Add-on-packages-in-R This gives the list of current _standard_ packages] If the question relates to a contributed package , e.g., one downloaded from CRAN, try contacting the package maintainer first. You can also use find("functionname") and packageDescription("packagename") to find this information. Only send such questions to R-help or R-devel if you get no reply or need further assistance. This applies to both requests for help and to bug reports." Note that RStudio maintains its own help resources at: https://community.rstudio.com/ This is where questions about the TidyVerse, ggplot, etc. should be posted. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Jan 24, 2022 at 4:14 PM Kai Yang via R-help <r-help at r-project.org> wrote:> > Hello Team, > I can run the function below: > > library(tidyverse) > > f2 <- function(indata, subgrp1){ > indata0 <- indata > temp <- indata0 %>% select({{subgrp1}}) %>% arrange({{subgrp1}}) %>% > group_by({{subgrp1}}) %>% > mutate(numbering =row_number(), max=max(numbering)) > view(temp) > f_table <- table(temp$Species) > view(f_table) > return(f_table) > } > f2(iris, Species) > > You can see the second argument I use Species only, and it works fine. > But If I say, I want the 2nd argument = Petal.Width, Species , how should I write the argument? I did try f2(iris, c(Petal.Width, Species)), but I got error message: > Error: arrange() failed at implicit mutate() step. > * Problem with `mutate()` column `..1`. > i `..1 = c(Petal.Width, Species)`. > i `..1` must be size 150 or 1, not 300. > > I'm not sure how to fix the problem either in function or can fix it when using the function. > Thank you, > Kai > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hallo You should explain better what do you want as many people here do not use tidyverse functions. I am not sure what the function should do. table(iris$Species) give the same result and whatever you do in tidyverse part it always finalise in table(...$Species) Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Kai Yang via R-help > Sent: Tuesday, January 25, 2022 1:14 AM > To: R-help Mailing List <r-help at r-project.org> > Subject: [R] function problem: multi selection in one argument > > Hello Team, > I can run the function below: > > library(tidyverse) > > f2 <- function(indata, subgrp1){ > indata0 <- indata > temp <- indata0 %>% select({{subgrp1}}) %>% arrange({{subgrp1}}) %>% > group_by({{subgrp1}}) %>% > mutate(numbering =row_number(), max=max(numbering)) > view(temp) > f_table <- table(temp$Species) > view(f_table) > return(f_table) > } > f2(iris, Species) > > You can see the second argument I use Species only, and it works fine. But > If I > say, I want the 2nd argument = Petal.Width, Species , how should I write the > argument? I did try f2(iris, c(Petal.Width, Species)), but I got error > message: > Error: arrange() failed at implicit mutate() step. > * Problem with `mutate()` column `..1`. > i `..1 = c(Petal.Width, Species)`. > i `..1` must be size 150 or 1, not 300. > > I'm not sure how to fix the problem either in function or can fix it when > using the > function. > Thank you, > Kai > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hello, Here are 3 functions that do what the question asks for. The first 2 are tidyverse solutions, the last one a base R function. I don't understand why the group_by and mutate, that's why I've included a 2nd tidyverse function. And the base R function follows the same lines of outputing the table only without data transformation. And the test dataset is not iris, it only has one factor variable. It doesn't make sense to table a continuous variable such as Petal.Width. The data set mtcars has several numeric variables that are in fact categorical ("vs", "am") and one, "cyl", that can be tabled. The examples below use mtcars. f3 <- function(data, ...){ ? groups <- unlist(list(...)) ? temp <- data %>% ??? select(all_of({{groups}})) %>% ??? group_by(across(all_of({{groups}}))) %>% ??? mutate(numbering = row_number(), max = n()) ? temp %>% ??? select(all_of({{groups}})) %>% ??? table() } f4 <- function(data, ...){ ? groups <- unlist(list(...)) ? data %>% ??? select(all_of({{groups}})) %>% ??? table() } f5 <- function(data, ...){ ? temp <- lapply(list(...), \(col) data[[col]]) ? table(setNames(temp, list(...))) } f3(mtcars, "am", "cyl") f4(mtcars, "am", "cyl") f5(mtcars, "am", "cyl") Hope this helps, Rui Barradas ?s 00:14 de 25/01/2022, Kai Yang via R-help escreveu:> Hello Team, > I can run the function below: > > library(tidyverse) > > f2 <- function(indata, subgrp1){ > ? indata0 <- indata > ? temp? ? <- indata0 %>% select({{subgrp1}}) %>% arrange({{subgrp1}}) %>% > ? ? group_by({{subgrp1}}) %>% > ? ? mutate(numbering =row_number(), max=max(numbering)) > ? view(temp) > ? f_table <- table(temp$Species) > ? view(f_table) > ? return(f_table) > } > f2(iris, Species) > > You can see the second argument I use Species only, and it works fine. > But If I say, I want the 2nd argument =?Petal.Width, Species , how should I write the argument? I did try?f2(iris, c(Petal.Width, Species)), but I got error message: > Error: arrange() failed at implicit mutate() step. > * Problem with `mutate()` column `..1`. > i `..1 = c(Petal.Width, Species)`. > i `..1` must be size 150 or 1, not 300. > > I'm not sure how to fix the problem either in function or can fix it when using the function. > Thank you, > Kai > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.