R 汇总特定的行集合
我想总结每个项目(多行)的数据,以便删除没有任何类型促销的项目。请参见示例数据R 汇总特定的行集合,r,R,我想总结每个项目(多行)的数据,以便删除没有任何类型促销的项目。请参见示例数据 item_code vendor_code launch_month unit_price department_name category_name 1 I-111164 V10 2007.M01 118 Face Face Treatment 2 I-111164 V10 2007.M01 118
item_code vendor_code launch_month unit_price department_name category_name
1 I-111164 V10 2007.M01 118 Face Face Treatment
2 I-111164 V10 2007.M01 118 Face Face Treatment
3 I-111164 V10 2007.M01 118 Face Face Treatment
4 I-111164 V10 2007.M01 118 Face Face Treatment
5 I-111164 V10 2007.M01 118 Face Face Treatment
6 I-111164 V10 2007.M01 118 Face Face Treatment
subcategory_name sales_velocity sales_month sales_unit promotion_type
1 Face Treatment Other B 2008.M01 41.00000 no_promotion
2 Face Treatment Other B 2008.M02 55.00000 no_promotion
3 Face Treatment Other B 2008.M03 64.80000 Catalogue Promotion
4 Face Treatment Other B 2008.M04 46.00000 no_promotion
5 Face Treatment Other B 2008.M05 67.00000 no_promotion
6 Face Treatment Other B 2008.M06 58.40000 Catalogue Promotion
>
在R中执行此操作的最佳实践方法是什么?以下命令返回一个数据帧,其中没有任何类型提升的行:
dat[dat$promotion_type != "no_promotion", ]
其中,dat
是数据帧的名称。库(dplyr)
library(dplyr)
item_code <- rep(c("I-111164"), each = 1, times = 6)
vendor_code <- rep(c("V10"), each = 1, times = 6)
launch_month <- rep(c("2007.M01"), each = 1, times = 6)
unite_price <- rep(c("118"), each = 1, times = 6)
department_name <- rep(c("Face"), each = 1, times = 6)
category_name <- rep(c("Face Treatment"), each = 1, times = 6)
subcategory_name <- rep(c("Face Treatment other"), each = 1, times = 6)
sales_velocity <- rep(c("B"), each = 1, times = 6)
sales_month <- rep(c("2008.M01"), each = 1, times = 6)
sales_unit <- rep(c(41,55,64,46,67,58), each = 1, times = 1)
promotion_type <- c("no_promotion", "no_promotion", "catalogue promotion",
"no_promotion", "no_promotion", "catalogue promotion")
# Create the data frame
foo <- data.frame(item_code, vendor_code, launch_month, unite_price, department_name,
category_name, subcategory_name, sales_velocity, sales_month,sales_unit, promotion_type, stringsAsFactors = F)
# Remove all rows with 'no_promotion'
foo2 <- filter(foo, promotion_type != "no_promotion")
# Get mean of sales unit for each item code
america <- foo2 %>%
group_by(item_code) %>%
summarize(sales = mean(sales_unit))
america
item_code首先,您要获取所有曾获得促销的商品的商品代码。假设您的数据帧被称为df
,请使用
got.promoted <- df$item_code[df$promotion_type != "no_promotion"]
got.promotion你也可以做dat[!grepl(“no_promotion”,dat$promotion\u type),]
got.promoted <- unique(got.promoted)
new.df <- df[df$item_code %in% got.promoted, ]