0

I have a dataset that I used group as to filter and now I am trying to expand back out the rows that I originally needed to consolidate in order to filter. The data was of a set of sales points by sales date. There is a row for each sales point but many different sales points for the same type of item. So I grouped by item in order to get important information like total sales and average sales price, etc. Now, after I have filtered and found the items with the correct sales frequency etc., I need to ungroup the data in order to see all of the sales points. I have tried using "ungroup()", but either it is not working or I am not doing it right.

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
  • 1
    You store the results into `blue_chip_index`, but then you pipe into that, as if it were a function. Is that intentional? – r2evans Jul 22 '19 at 23:49
  • Keep in mind that we don't have any of your data, so we can't run your code, and we can't see any output, so it's hard to guess what's going on. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R post that's easier to help with – camille Jul 23 '19 at 03:09

1 Answers1

0

If I understand what you want, it sounds like you should just start with the original data frame again, then filter on the items that you want.

Ungrouping the new data frame won’t allow you to unsummarize it.

Does this achieve want you want:

library(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)
Joe
  • 662
  • 1
  • 7
  • 20
  • thanks joe, I think that's gonna work. Is there anyway you can explain what makes this code work? – rnoob12345 Jul 23 '19 at 19:12
  • Sorry, I don’t come on the Internet often. You grouped by ‘graded_title’ and used summarize to compute ‘Market_Cap’, then filtered for the ‘graded_titles’ that had ‘Market_Cap’ > 10000 and count >25. All I did was save that to ‘blue_chip_index’, then filter ‘top25000’ to only include ‘graded_title’s in the ‘blue_chip_index’. – Joe Jul 26 '19 at 00:47