R Equivalent of Stata's Collapse Command: Summarizing Data by Department

R Equivalent of Stata’s Collapse Command: Summarizing Data by Department

As a Stata user, I’ve often relied on the collapse command to summarize data by categorical variables. But what about in R? If you’re working with budget data by line item and department, how can you sum at the department level?

It turns out that R has several options to achieve this. One popular approach is to use the dplyr package, specifically the group_by and summarise functions. By grouping your data by department and then summarising the line items, you can easily calculate the sum at the department level.

Another option is to use the aggregate function, which can also be used to summarize data by one or more categorical variables.

If you’re new to R or coming from a Stata background, it’s worth exploring these different approaches to find what works best for your workflow.

What’s your go-to method for summarizing data in R? Share your thoughts in the comments!

Leave a Comment

Your email address will not be published. Required fields are marked *