Calculate Percentage by Group in R
In this article, we will learn how to calculate percentage by group in a dataset in R programming. It is done using the dplyr library. We will first create a dataset and then calculate the percentage by group. We will also look at how to format these percentages.
Dataset
Let’s say an investor maintains two portfolios, A and B, with investment in certain stocks in each portfolio. In R, we can have this dataset in the form of a data frame.
Our Dataframe
1 portfolios <- data.frame(portfolio=c('Portfolio A', 'Portfolio A', 'Portfolio A',
portfolios
portfolio stock amount
