【发布时间】:2021-08-13 13:10:18
【问题描述】:
我正在尝试使用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
接下来,我将product id 和total sales 列分别重命名为Product Code 和Net Sales,并在Net Sales 上应用我自己的函数mutate() 并创建一个新的Volume 列。
> data2 <- data1 %>%
+ select(`Product Code` = `product id`, `Net Sales` = `total sales`) %>%
+ replace_na(list(`Net Sales` = 0))%>%
+ arrange(desc(`Net Sales`))%>%
+ mutate(Volume = rank_volume(data1, `Net Sales`))
这是我收到的错误消息:
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_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")
}
当我使用一个简单的数据框单独运行我的函数时,它可以完美运行。但是,当我使用 mutate() 运行它时,出现了错误。我找不到问题。有人可以帮忙吗?
编辑: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"))
【问题讨论】:
-
您可以通过
dput(head(data))分享您的可重现数据吗? -
您在 mutate 中调用 data1 而 data1 没有 Net Sales 列
-
@BastienDucreux 我在清理过程中将总销售额的名称更改为净销售额。
mutate()是否采用初始 data1 代替?当我将其更改为mutate(Volume = rank_volume(data1, `total sales`))时它可以工作 -
@AnoushiravanR 我现在在编辑中添加了
dput(head(data))。