Dplyr top 5 product by ID

Hi R com.

I have this piece of code:

Distribution of sales pr. salesId

df1 %>%
group_by(ItemId) %>%
mutate(SalesAmount = SalesPrice*SalesQty) %>%
summarize(sum_sale = sum(SalesAmount, na.rm = TRUE)) %>%
ggplot() +
geom_bar(aes(x = reorder(ItemId, sum_sale, FUN = desc),
y = sum_sale), stat = "identity") +
theme_bw() +
xlab("ItemID") +
ylab("Sum Sales")

But i have like 1000+ products and all i want is to look at the top 10 ItemId, the reorder function is not really helping me here :slight_smile: unless there is a cool way of taking the top 10 Itemid based on SalesAmount and sort them. Any ideas?

df1 %>%
  group_by(SalesId) %>%
  mutate(SalesAmount = SalesPrice*SalesQty) %>%
  summarize(sum_sale = sum(SalesAmount, na.rm = TRUE)) %>%
  arrange(-SalesAmount) %>%
  head(10)
1 Like

Hi arthur.t

df1 %>%
group_by(SalesId) %>%
mutate(SalesAmount = SalesPrice*SalesQty) %>%
summarize(sum_sale = sum(SalesAmount, na.rm = TRUE)) %>%
arrange(-SalesAmount) %>%
head(10)

gives the error :
Error: arrange() failed at implicit mutate() step.

  • Problem with mutate() column ..1.
    i ..1 = -SalesAmount.
    x object 'SalesAmount' not found

what does that mean?? SalesAmount is a column in the df

Have corrected the original post. And just to summarize: what i am interested in is seeing the top 10 itemID based on the total sales

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.