grouped_df_impl(data,unname(vars),drop)中的错误: [英] Error in grouped_df_impl(data, unname(vars), drop) :

查看:65
本文介绍了grouped_df_impl(data,unname(vars),drop)中的错误:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试编写一个内部有2个管道的函数,以便当我给定名称x和变量名称y时,结果将被聚集,加入数据并被删除。当我运行编写的函数时,标题错误消息不断出现,我不知道问题出在哪里。

This is the first time I try to write a function with 2 pipes inside so that when I give a name x and variable name y, the results will be aggregrated, joined to the data and removed. When I run my written function, the captioned error message kept on coming up and I have no idea what the problem is.

mrr <- function(x, y){ 
  x <- data %>%
   group_by(y) %>%
   summarize(n=n(),
         sum=sum(unit_sales)) 
data <- data %>%
  left_join(x, by="y") %>%
  mutate(str_c(x,y))=(sum-unit_sales)/(n-1) %>%
  select(-one_of(n, sum)) %>%
  rm(x)
}

当我在搜索框中给出错误消息时,我找到了相关的结果,但答案与我的问题。

I found the related results when I gave the error message in the search box, but the answers were not related to my problem. Do let me know if you have any thoughts.

推荐答案

这里有很多事情。

首先,您不需要在函数中输入x,因为您会立即覆盖它。我不确定您是否打算输入数据。

First, you don't need to input x into the function...because you are overwriting it immediately. I'm not sure whether you may have meant to input data.

接下来,您的group_by无法正常工作,因为y不存在于数据集中... y的内容是标题的名称。我们可以使用group_by_()解决此问题。

Next, your group_by isn't working because y doesn't exist in your data set...the contents of y are the name of the header. We can use group_by_() to solve this problem.

在那之后,您的mutate命令被破坏了。 mutate(str_c(x,y))是完整的mutate语句,因为()已关闭。另外,str_c(x,y)试图获取一个数据框x并在y内与一个字符连接以取出单个字符...这充其量只会给您一个数据框,更糟糕的是会出现错误。最好只指定一个列名。

After that, your mutate command is broken. mutate(str_c(x,y)) is a complete mutate statement because the () are closed. Also, str_c(x,y) is trying to take a dataframe x and concatenate with a character inside y to get a single character out...this will at best give you a dataframe out and at worse give an error. Better to just specify a column name.

在同一变异中,您可能会遇到 =(sum-unit_sales)/(n-1)。如果n = 1,则得到除以零的错误输出 NaN 。您可以根据需要保留此选项。只需了解方程式的实际作用即可。

In the same mutate, you may run into an error in =(sum-unit_sales)/(n-1). If n = 1 then you get a divide by zero error output of NaN. You can leave this if you want. Just understand what your equation is actually doing.

渐渐接近,-one_of()命令需要一个字符向量(不仅仅是列名。您可以执行 select(-n,-sum)或执行 select(-one_of(c( n, sum)))

Getting close, the -one_of() command requires a vector of characters (not just column names. You can either do select(-n,-sum) or you can do select(-one_of(c("n", "sum"))).

最后,您实际上并不需要使用 rm(x),因为变量将

Finally, you don't really need to use rm(x) because the variable will be removed when the function is done running anyway.

我不得不猜测列的名称...您的名字将有所不同,因为您没有给我一个样本数据集。

I had to guess at the name of columns...your names will be different since you didn't give me a sample data set.

data <- data.frame(d1 = runif(n=10,min=1,max=10),
           d2 = runif(n=10,min=1,max=10),
           unit_sales = runif(n=10,min=1,max=10))

mrr <- function(data, y){ 
  x <- data %>%
    group_by_(.dots = y) %>%
    summarize(n=n(),
     sum=sum(unit_sales)) 
  data <- data %>%
  left_join(x, by=y) %>%
    mutate(someCol=(sum-unit_sales)/(n)) %>%
    select(-one_of(c("n", "sum"))) #%>%
    # rm(x)
}

(mrr(data,"d2"))

这篇关于grouped_df_impl(data,unname(vars),drop)中的错误:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆