Re: [R] function problem: multi selection in one argument

2022-01-25 Thread Martin Maechler
> Rui Barradas 
> on Tue, 25 Jan 2022 14:22:47 + writes:

> 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

Thank you, Rui!

Note that your base R solution can be vastly simplified :

> f6 <- function(data, ...) table(data[, unlist(list(...))])
> f6(mtcars, "am", "cyl")
   cyl
am   4  6  8
  0  3  4 12
  1  8  3  2
> 

If you started measuring carefully, I'm sure this would not only
be the shortest but also by far the fastest solution ...

Best,
Martin

--
Martin Maechler
ETH Zurich  and   R Core Team


> À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@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.

> __
> R-help@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.

__
R-help@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.


Re: [R] function problem: multi selection in one argument

2022-01-25 Thread Rui Barradas

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@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.


__
R-help@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.


Re: [R] function problem: multi selection in one argument

2022-01-25 Thread PIKAL Petr
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  On Behalf Of Kai Yang via R-help
> Sent: Tuesday, January 25, 2022 1:14 AM
> To: R-help Mailing List 
> 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@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.
__
R-help@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.


[R] function problem: multi selection in one argument

2022-01-24 Thread Kai Yang via R-help
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@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.


[R] function problem

2009-07-31 Thread RRRRRRRRRR!

I have a series of columns that need to be evaluated in various tables. I
need to apply a function in the following manner somers2(name1,name2). name1
is a vector of x inputs for the function which correspond to a vector of y
inputs in name2.

y-rep(c(3,4,5,8),6)
z-rep(c(23,24,25,26,27,28),4)
name1-sprintf(Pred_pres_%s_indpdt[,%s,,],x,y)
name2-sprintf(population[,%s],z)


rank-function(i,j){

for(i in name1){
for(j in name2){
somers2(i,j)
}}}

Thanks

Robert
-- 
View this message in context: 
http://www.nabble.com/function-problem-tp24759253p24759253.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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.


Re: [R] function problem

2009-07-31 Thread Steve Lianoglou

Hi,

On Jul 31, 2009, at 12:25 PM, RR! wrote:

I have a series of columns that need to be evaluated in various  
tables. I
need to apply a function in the following manner  
somers2(name1,name2). name1
is a vector of x inputs for the function which correspond to a  
vector of y

inputs in name2.

y-rep(c(3,4,5,8),6)
z-rep(c(23,24,25,26,27,28),4)
name1-sprintf(Pred_pres_%s_indpdt[,%s,,],x,y)
name2-sprintf(population[,%s],z)


rank-function(i,j){

for(i in name1){
for(j in name2){
somers2(i,j)
}}}


You're missing a value for x, so I'm finding it hard to understand  
what you're trying to do, exactly. From your double for loop, it seems  
like you just want to compute the function over all vals in X, and  
each x against all Y.


Does this help?

R x - LETTERS[1:3]
R y - LETTERS[10:15]
R all.vals - expand.grid(x,y, stringsAsFactors=F)
R all.vals[1:3,]
  Var1 Var2
1AJ
2BJ
3CJ

R ans - lapply(seq(nrow(all.vals)), function(i) paste(all.vals[i,1],  
all.vals[i,2], sep='(*)'))

R ans[1:3]
[[1]]
[1] A(*)J

[[2]]
[1] B(*)J

[[3]]
[1] C(*)J

-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
  |  Memorial Sloan-Kettering Cancer Center
  |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

__
R-help@r-project.org mailing list
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.


Re: [R] function problem

2009-07-31 Thread RRRRRRRRRR!

I am sorry I should have stated the whole problem:

I appreciate all the help I have received from this list and also not being
flamed because I am new to R. Many of my problems are in automation so far.

I am trying to create multiple objects that are outputs of functions. Here
are the tasks:

aGAM-somers2(Pred_pres_a_indpdt[,3,,],population[,23])
aGBM-somers2(Pred_pres_a_indpdt[,4,,],population[,23])
cGLM-somers2(Pred_pres_a_indpdt[,5,,],population[,23])
cRF-somers2(Pred_pres_a_indpdt[,8,,],population[,23])

bGAM-somers2(Pred_pres_b_indpdt[,3,,],population[,24])
bGBM-somers2(Pred_pres_b_indpdt[,4,,],population[,24])
bGLM-somers2(Pred_pres_b_indpdt[,5,,],population[,24])
bRF-somers2(Pred_pres_b_indpdt[,8,,],population[,24])

...and so on through the letter f.

so the variables (x,y,z in this example) are as follows:

xGAM-somers2(Pred_pres_x_indpdt[,y,,],population[,z])

the GAM, GBM, GLM, and RF are stored in a column y in Pred_pres_x_indpdt.
x is denoting a species, which corresponds to column z in population, so
a:f == 23:28

~~~
This may be pushing it, but it would be great if I could get all of these in
a matrix of dim[1,96]

Here is what I have so far:

x - c(a,b,c,d,e,f)
nums - c(23:28)

rank-function(x){

for(y in nums){

xGAM-somers2(Pred_pres_x_indpdt[,3,,],population[,y])
xGBM-somers2(Pred_pres_x_indpdt[,4,,],population[,y])
xGLM-somers2(Pred_pres_x_indpdt[,5,,],population[,y])
xRF-somers2(Pred_pres_x_indpdt[,8,,],population[,y])

rank(x)

}}

#for each species, there is 16 columns of output, so a total of 96 columns
is needed

x_matrix-matrix(c(xGAM[1:2],Evaluation.results.TSS[[1]][1,3:4],xGBM[1:2],Evaluation.results.TSS[[1]][2,3:4],
xGLM[1:2],Evaluation.results.TSS[[1]][3,3:4],xRF[1:2],Evaluation.results.TSS[[1]][4,3:4]),
nrow=1,ncol=16,
dimnames=list(c(),
c(x_gam_AUC, x_gam_Dxy,x_gam_TSS,x_gam_Cutoff,x_gbm_AUC,
x_gbm_Dxy,x_gbm_TSS,x_gbm_Cutoff,
x_glm_AUC, x_glm_Dxy,x_glm_TSS,x_glm_Cutoff,x_rf_AUC,
x_rf_Dxy,x_rf_TSS,x_rf_Cutoff)))


Thanks again for helping me out



Steve Lianoglou-6 wrote:
 
 Hi,
 
 On Jul 31, 2009, at 12:25 PM, RR! wrote:
 
 I have a series of columns that need to be evaluated in various  
 tables. I
 need to apply a function in the following manner  
 somers2(name1,name2). name1
 is a vector of x inputs for the function which correspond to a  
 vector of y
 inputs in name2.

 y-rep(c(3,4,5,8),6)
 z-rep(c(23,24,25,26,27,28),4)
 name1-sprintf(Pred_pres_%s_indpdt[,%s,,],x,y)
 name2-sprintf(population[,%s],z)


 rank-function(i,j){

  for(i in name1){
  for(j in name2){
 somers2(i,j)
 }}}
 
 You're missing a value for x, so I'm finding it hard to understand  
 what you're trying to do, exactly. From your double for loop, it seems  
 like you just want to compute the function over all vals in X, and  
 each x against all Y.
 
 Does this help?
 
 R x - LETTERS[1:3]
 R y - LETTERS[10:15]
 R all.vals - expand.grid(x,y, stringsAsFactors=F)
 R all.vals[1:3,]
Var1 Var2
 1AJ
 2BJ
 3CJ
 
 R ans - lapply(seq(nrow(all.vals)), function(i) paste(all.vals[i,1],  
 all.vals[i,2], sep='(*)'))
 R ans[1:3]
 [[1]]
 [1] A(*)J
 
 [[2]]
 [1] B(*)J
 
 [[3]]
 [1] C(*)J
 
 -steve
 
 --
 Steve Lianoglou
 Graduate Student: Computational Systems Biology
|  Memorial Sloan-Kettering Cancer Center
|  Weill Medical College of Cornell University
 Contact Info: http://cbio.mskcc.org/~lianos/contact
 
 __
 R-help@r-project.org mailing list
 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.
 
 

-- 
View this message in context: 
http://www.nabble.com/function-problem-tp24759253p24759625.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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.