Sort, sum, and add percentages after "count" in R

117 Views Asked by At

I have a straightforward code in R that gives me the number of times a username sent a ticket request.

Data is the source dataframe, and I subsetted by request type "4"

data2 <- subset(data, q_1 == '4')

data_df_2 <- count(data2, "ticket_holder_username")

data_df_2

Attached is the image of the output in RMD. (blacked out names for privacy) Output

  1. How do I sort the output by the frequency?

  2. How do I add another column that shows the percentage out of total tickets?

  3. Is there a way to have a "Sum" of counts display at the bottom?

I am welcome to using a different package if it could give a neater output.

I have tried

arrange(desc())

but it gives is.data.frame(df) is not true.

EDIT:

Here's what I tried per https://stackoverflow.com/users/19340922/nan-yang suggestion:

dplyr code

1

There are 1 best solutions below

2
nan Yang On

you can use the function group_by and summarise in the dplyr package. In your case, you can count the frequency using this:

data_df_2 <- data2 %>% 
group_by(ticket_holder_username) %>%
mutate(count = n()) 

Then you can sort it from highest to the lowest using summarise

data_df_2 <- data2 %>% 
group_by(ticket_holder_username) %>%
mutate(count = n()) %>%
summarise(desc(count)) 

To add another column showing the percentage:

data_df_2 <- data2 %>% 
    group_by(ticket_holder_username) %>%
    mutate(count = n()) %>%
    summarise(desc(count)) %>%
    mutate(percent = count/sum(count))