0

I have this data.frame:

structure(list(Date = structure(c(1609027200, 1609027200, 1609027200, 1609027200, 1609027200, 1609027200),
                                tzone = "UTC", class = c("POSIXct", "POSIXt")),
               Type = c("In Store", "In Store", "In Store", "In Store", "In Store", "In Store"),
               Method = c("Roadie", "Roadie", "Roadie", "Roadie", "Roadie", "Roadie"), 
               `Category Full` = c("SAFES", "WOOD PELLETS", "FEEDLOT PANELS", "S&H OR HOME DELIVERY", "T-POSTS", "S&H OR HOME DELIVERY" ), 
               Week = c(52, 52, 52, 52, 52, 52)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))

My code is this:

res02 <- d %>%
select(Date, Method, Category_Full ='Category Full') %>%
mutate(Week = week(Date)) %>%
group_by(Week, Category_Full) %>%
mutate(Category_Count = nchar(Category_Full))

enter image description here

I am trying to find the top 5 $Category_Full base on $Method for each Week of the year. The Week Column lists 1:52. I tried using nchar and summarise but I can't get this to work

The Excel sheet would be similar end result

enter image description here

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
Zane Cantrell
  • 247
  • 3
  • 9
  • Why don't you want to use summarize? – ktiu Jun 16 '21 at 14:02
  • When I used summarise, it automatically ungroups() my dataframe – Zane Cantrell Jun 16 '21 at 14:04
  • 2
    Your description is not clear to me. What is your desired output? It would be helpful if you could provide us with a reproducible [minimal working example](https://en.wikipedia.org/wiki/Minimal_working_example) that we can copy and paste to better understand the issue and test possible solutions. You can share datasets with `dput(YOUR_DATASET)` or smaller samples with `dput(head(YOUR_DATASET))`. (See [this answer](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610) for some great advice.) – ktiu Jun 16 '21 at 14:07
  • structure(list(Date = structure(c(1609027200, 1609027200, 1609027200, 1609027200, 1609027200, 1609027200), tzone = "UTC", class = c("POSIXct", "POSIXt")), Type = c("In Store", "In Store", "In Store", "In Store", "In Store", "In Store"), Method = c("Roadie", "Roadie", "Roadie", "Roadie", "Roadie", "Roadie"), `Category Full` = c("SAFES", "WOOD PELLETS", "FEEDLOT PANELS", "S&H OR HOME DELIVERY", "T-POSTS", "S&H OR HOME DELIVERY" ), Week = c(52, 52, 52, 52, 52, 52)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")) – Zane Cantrell Jun 16 '21 at 14:14
  • Trying to group_by() Type and Method, then find the top Category in those groups for every week of the year. Looking at trends of items that I sell. – Zane Cantrell Jun 16 '21 at 14:16
  • 1
    Please edit your question with reproducible data. Do not place it in the comments because it cannot be properly formatted there. The `summarize()` function has a `.groups=` parameter. You can use `.groups="keep"` not to drop groups. – MrFlick Jun 16 '21 at 16:27

0 Answers0