I have a function that is meant to return the top 10 customers in a DF, however, the output of my function has the unaltered DF (asides from groupings) with results out of order and also displaying every single row, despite the fact that I asked for top 10.
myData <- data.frame("Platform" =
c("Digital", "Digital", "Digital", "Digital", "Digital", "Digital", "Offline", "Offline", "Offline", "Offline", "Offline"), "Cust.ID" = c("John", "Josh", "Juan", "Jason", "Jay", "Jorge", "Jurgen", "Julian", "Jules", "James", "Jimmy"),
"Input.Records" = c(100, 150, 102, 10, 111, 132, 125, 154, 101, 103, 100))
getTopTenCustomers <- function(platformFilter = "Digital"){
filteredDf <- myData %>% group_by(Cust.ID, Platform) %>%
filter(Platform == platformFilter) %>% summarise(Input.Records = sum(Input.Records)) %>%
top_n(10) %>% arrange(myData, desc(Input.Records))
data.frame(filteredDf)
return(filteredDf)
}
Also, I'm a bit concerned about that arrange(myData, desc(Input.Records))
call. Isn't that just arranging the rows in the original DF? Every other example I saw had the arrange at first, but that also seems concerning because, the way it seems, sorting the values before grouping and filtering might result in the filter displaying inaccurate results, wouldn't it?