在R中的用户定义函数中传递多个参数?

在R中的用户定义函数中传递多个参数?,r,dplyr,R,Dplyr,我的目标是创建一个用户定义的函数,该函数将数据框、月份或年份作为x,产品类别作为y,并返回一个数据框,其中包含城市前10名客户群 我不想把城市当作一个论据 toptencust <- function(df,x,y){ library(magrittr) library(dplyr) ifelse(is.character(x) , df %>% select_(City,Amount,Customer,Product,Year,month) %

我的目标是创建一个用户定义的函数,该函数将数据框、月份或年份作为x,产品类别作为y,并返回一个数据框,其中包含城市前10名客户群

我不想把城市当作一个论据

 toptencust <- function(df,x,y){
  library(magrittr)
  library(dplyr)

  ifelse(is.character(x)
    , df %>% 
      select_(City,Amount,Customer,Product,Year,month) %>%
      group_by_(City,Customer) %>%
      filter_(month==x & Product==y) %>% 
      summarise_(Tot_repay=sum(Amount,na.rm=T)) %>% 
      top_n(n=10)
    , df %>% 
      select_(City,Amount,Customer,Product,Year,month) %>%
      group_by_(City,Customer) %>%filter_(Year==x& Product==y) %>%
      summarise_(Tot_repay=sum(Amount,na.rm=T)) %>% 
      top_n(n=10)
    )

}
总和(Amount,na.rm=T)错误:参数的“类型”(符号)无效


我无法理解此问题,请帮助?

执行您的示例时,我遇到另一个错误:

compat_lazy_dots(.dots,caller_env(),…)中出错: 找不到对象“城市”

这是因为您使用了“转义图案填充”功能
选择
过滤
等等。您之所以这样做,可能是因为您需要在
filter\中使用变量
x
(month==x&Product==y)
。但是现在像
Product
这样的其他名称也被视为变量,它们是数据帧中的名称

这是一个

现在,使用
解决这个问题的方式有所不同操作员。看这个小插曲

toptencust%
选择(城市、金额、客户、产品、年、月)%>%
分组依据(城市、客户)%>%
过滤器(月份=!!x和产品=!!y)%>%
总结(偿还总额=总额(不适用,rm=T))%>%
顶部(n=10)
,df%>%
选择(城市、金额、客户、产品、年、月)%>%
分组依据(城市、客户)%>%
过滤器(年份=!!x和产品=!!y)%>%
总结(偿还总额=总额(不适用,rm=T))%>%
顶部(n=10)
)
}

一点线索也没有。合并中有什么内容?我建议你读一下,然后编辑你的问题。但是有一些提示:(1)不要在这里使用
if_else
,只要
if{…}else{…}
,这是一个非常错误/低效的向量化条件函数的使用;(2) 不要将整个
magrittr
管道放在一行上,这会使阅读和调试更加困难。请阅读我在第一条评论中提供的链接。很明显,它是一个框架或类似框架的东西,因此可以在
dplyr
-管道中使用。如果您希望有人能够解决正在发生的问题,您需要提供更多。减少这个问题也很好,因为我怀疑我们不需要处理所有这些专栏就能解决您的问题。另一个很好的参考资料:。代码对我不起作用,因为您提供了png,但是
dplyr
函数需要
data.frame
。(你复制
dput(head(mydataset))
的输出可能和屏幕截图一样容易,但如果我们可以将数据复制/粘贴到R会话中,我们测试数据就容易100倍。我不打算转录它。)Anubhav,我建议你读两遍,你更接近了,但是你没有做任何推荐的方法。他们建议(非常明确地)以易于使用的格式提供数据,其中两个很好的例子是
dput(head(x))
read.table(text=“…”)
。为了证明我的观点,请尝试快速、轻松地读取您发布到data.frame中的数据,现在对我刚刚编辑的问题执行相同的操作(使用
read.table
)。这样容易多了。
df <- read.table(header=TRUE, stringsAsFactors=FALSE, text="
Customer    Date        Amount  month     City        Product  Year
A1          12/01/04    495415  January   BANGALORE   Gold     2004
A1          03/01/04    245899  January   BANGALORE   Gold     2004
A1          15/01/04    259490  January   BANGALORE   Gold     2004
A1          25/01/04    437555  January   BANGALORE   Gold     2004
A1          17/01/05    165973  January   BANGALORE   Gold     2005
A1          23/02/05    365367  February  BANGALORE   Gold     2005
A1          01/02/05    14473   February  BANGALORE   Gold     2005
A8          05/02/04    100002  February  PATNA       Silver   2004
A9          28/02/05    100003  February  CHENNAI     Silver   2005
A10         16/02/05    48759   February  CALCUTTA    Gold     2005
A11         23/02/05    208318  February  COCHIN      Gold     2005
A12         03/02/05    150281  February  BOMBAY      Gold     2005
A13         04/02/06    339078  February  BANGALORE   Gold     2006
A14         25/03/06    137835  March     BANGALORE   Gold     2006
A15         31/03/06    437120  March     CALCUTTA    Gold     2006
A16         23/03/06    103924  March     COCHIN      Gold     2006
A17         19/03/04    408467  March     BOMBAY      Gold     2004
A18         05/03/06    100000  March     BANGALORE   Silver   2006
A19         04/04/05    10000   April     BANGALORE   Platinum 2005
A20         30/04/06    10001   April     CALCUTTA    Platinum 2006
A21         25/04/04    10002   April     COCHIN      Platinum 2004
A22         19/04/06    100000  April     BOMBAY      Silver   2006
A23         06/04/04    80346   April     BANGALORE   Silver   2004
A24         27/04/05    100002  April     DELHI       Silver   2005
A25         05/05/04    100003  May       COCHIN      Silver   2004
A26         06/05/06    470982  May       PATNA       Gold     2006
A27         07/05/05    357376  May       CHENNAI     Gold     2005
A28         08/05/06    326050  May       TRIVANDRUM  Gold     2006
A29         09/05/05    215083  May       CALCUTTA    Gold     2005
A30         10/05/06    481343  May       BANGALORE   Gold     2006")
toptencust(df,'February',2014)
toptencust <- function(df,x,y){
    library(magrittr)
    library(dplyr)

    ifelse(is.character(x)
           , df %>% 
               select(City,Amount,Customer,Product,Year,month) %>%
               group_by(City,Customer) %>%
               filter(month == !!x & Product == !!y) %>% 
               summarise(Tot_repay=sum(Amount,na.rm=T)) %>% 
               top_n(n=10)
           , df %>% 
               select(City,Amount,Customer,Product,Year,month) %>%
               group_by(City,Customer) %>%
               filter(Year == !!x & Product == !!y) %>%
               summarise(Tot_repay=sum(Amount,na.rm=T)) %>% 
               top_n(n=10)
    )
}