# how to sum a variable by group in r and display in a tabular form?

Say I have this data

User 1  User 2  Duration
5       2       7-11 months
5       5       Less than 1 month
5       5       4-6 months
5       5       4-6 months
5       4       4-6 months
5       5       2 years
4       6       1 year
4       4       4-6 months
4       8       7-11 months
4       4       2 years
3       3       Less than 1 month
3       3       7-11 months
3       3       1-3 months


To sum the variable, for example, if I want to calculate user1 within 4-6 months, I did that. Now I want to display all the individual values like

       4-6 months  7-11 months  1-3 months
User1    values      values       values
User2    values      values       values


How do I get this kind of output?

Till now I have used this

df %>% group_by(Duration) %>% summarise(count=sum(User1, na.rm = TRUE)) %>% filter(Duration=="4-6 months")

• You'll get more eyeballs on this question if you take it to stackoverflow.com where it is also on-topic. – Spacedman Aug 25 '17 at 16:45