【问题标题】:Need help ungrouping data frame需要帮助取消分组数据框
【发布时间】:2019-07-22 23:47:19
【问题描述】:

我有一个数据集,我使用 group 进行过滤,现在我正在尝试扩展我最初需要合并以进行过滤的行。数据是按销售日期划分的一组销售点。每个销售点都有一行,但同一类型的商品有许多不同的销售点。所以我按项目分组以获得总销售额和平均销售价格等重要信息。现在,在我过滤并找到具有正确销售频率等的项目后,我需要取消分组数据以便查看所有的销售点。我曾尝试使用“ungroup()”,但要么它不起作用,要么我做得不对。

top25000 <- read_csv("index_sales_export.csv")
blue_chip_index <- top25000 %>%
  select(graded_title,date,price,vcp_card_grade_id) %>%
  filter(date >= as.Date("2018-07-08")) %>%
  group_by(graded_title) %>%
   summarise(Market_Cap = sum(price),Average_Price = mean(price),VCP_Card_Grade_ID = mean(vcp_card_grade_id),count=n())%>%
  filter(Market_Cap >= 10000 & count >= 25)%>%
  rename(Total_Number_of_Sales = count)%>%
blue_chip_index

【问题讨论】:

  • 您将结果存储到blue_chip_index,然后您将其输入,就好像它是一个函数一样。这是故意的吗?
  • 请记住,我们没有您的任何数据,因此我们无法运行您的代码,也看不到任何输出,因此很难猜测发生了什么。 See here 撰写更易于帮助的 R 帖子

标签: r


【解决方案1】:

如果我了解您想要什么,听起来您应该重新从原始数据框开始,然后过滤您想要的项目。

取消分组新数据框不会让您取消汇总。

这是否实现了你想要的:

图书馆(tidyverse)

top25000 <- read_csv("index_sales_export.csv")
blue_chip_index <- top25000 %>%
  select(graded_title,date,price,vcp_card_grade_id) %>%
  filter(date >= as.Date("2018-07-08")) %>%
  group_by(graded_title) %>%
  summarise(Market_Cap = sum(price),
            Average_Price = mean(price),
            VCP_Card_Grade_ID = mean(vcp_card_grade_id),
            count=n())%>%
  filter(Market_Cap >= 10000 & count >= 25) %>%
  rename(Total_Number_of_Sales = count)


top25000 %>% 
  filter(graded_title %in% blue_chip_index$graded_title)

【讨论】:

  • 谢谢乔,我认为这会奏效。无论如何你可以解释是什么让这段代码工作?
  • 对不起,我不常上网。您按“graded_title”分组并使用汇总计算“Market_Cap”,然后过滤“Market_Cap”> 10000 且计数>25 的“graded_titles”。我所做的只是将其保存到“blue_chip_index”,然后过滤“top25000”以仅在“blue_chip_index”中包含“graded_title”。
猜你喜欢
  • 2021-01-16
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
相关资源
最近更新 更多