Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R mutate()返回错误';未找到对象';_R_Dplyr_Mutate - Fatal编程技术网

R mutate()返回错误';未找到对象';

R mutate()返回错误';未找到对象';,r,dplyr,mutate,R,Dplyr,Mutate,我正在尝试使用mutate()清理并向数据中添加一个名为Volume的新列 这是我读入R的数据: > df1 <- file.choose() > data1 <- read_excel(df1) > hea

我正在尝试使用
mutate()
清理并向数据中添加一个名为
Volume
的新列

这是我读入R的数据:

> df1 <- file.choose()
> data1 <- read_excel(df1)                                                                                                                                   
> head(data1)
# A tibble: 5 x 3
  `product id` amount `total sales`
  <chr>         <dbl>         <dbl>
1 X180             20           200
2 X109             30           300
3 X918             20           200
4 X273             15           150
5 X988             12           120
这是我收到的错误消息:

 Error: Problem with `mutate()` column `Volume`.
ℹ `Volume = rank_volume(data1, `Net Sales`)`.
x arrange() failed at implicit mutate() step. 
* Problem with `mutate()` column `..1`.
ℹ `..1 = Net Sales`.
x object 'Net Sales' not found
这是我创建的函数
rank\u volume

### a function to label the products that are top one third in total sales as "H", products with the lowest third in sales as "L", and the rest as "M"
rank_volume <- function(data, column) {
  
  column <- ensym(column)
  colstr <- as_string(column)
  data <- arrange(data, desc(!!column))
  size <- length(data[[colstr]])
  first_third <- data[[colstr]][round(size / 3)]
  last_third <- data[[colstr]][round(size - (size / 3))]
  
  case_when(data[[colstr]] > first_third ~ "H",
            data[[colstr]] < last_third ~ "L",
            TRUE ~ "M")
}

您可以在完成初始清理后添加新列

data2%
选择(“产品代码”=“产品id”,“净销售额”=“总销售额”)%>%
替换(列表(“净销售额”=0))%>%
安排(说明(“净销售额”))
数据2%
变更(数量=排名数量(数据2,“净销售额”))

data1
没有
净销售额
列,它存在于您所做的转换中。您可以使用
引用管道中的当前数据帧

library(dplyr)

data1 %>% 
     select(`Product Code` = `product id`, `Net Sales` = `total sales`) %>%
     replace_na(list(`Net Sales` = 0))%>%
     arrange(desc(`Net Sales`)) %>%
     mutate(Volume = rank_volume(., `Net Sales`))

# `Product Code` `Net Sales` Volume
#  <chr>                <dbl> <chr> 
#1 X109                   300 H     
#2 X180                   200 M     
#3 X918                   200 M     
#4 X273                   150 L     
#5 X988                   120 L     

您能否通过
dput(head(data))
共享一段可复制的数据?您在mutate中调用data1,而data1没有净销售额column@BastienDucreux在我的清理工作中,我把总销售额改为净销售额。
mutate()
是否取而代之的是初始数据1?当我将其更改为
mutate(Volume=rank\u Volume(data1,`total sales`))
@AnoushiravanR时,它就起作用了。我在现在的编辑中添加了
dput(head(data))
> dput(head(data1))
structure(list(`product id` = c("X180", "X109", "X918", "X273", 
"X988"), amount = c(20, 30, 20, 15, 12), `total sales` = c(200, 
300, 200, 150, 120)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))
library(dplyr)

data1 %>% 
     select(`Product Code` = `product id`, `Net Sales` = `total sales`) %>%
     replace_na(list(`Net Sales` = 0))%>%
     arrange(desc(`Net Sales`)) %>%
     mutate(Volume = rank_volume(., `Net Sales`))

# `Product Code` `Net Sales` Volume
#  <chr>                <dbl> <chr> 
#1 X109                   300 H     
#2 X180                   200 M     
#3 X918                   200 M     
#4 X273                   150 L     
#5 X988                   120 L     
data1 %>% 
     select(`Product Code` = `product id`, `Net Sales` = `total sales`) %>%
     replace_na(list(`Net Sales` = 0))%>%
     arrange(desc(`Net Sales`)) %>%
     mutate(Volume = rank_volume(cur_data(), `Net Sales`))