So essentially I am relatively new to R and my weak spot is writing as little code as possible. I always run into the same problem and I just can't seem to solve it with a loop or a function, so I'd love some help.
Let's say my df looks like this:
a = c(12, 9, 11, 17, 22)
b = c(8, 1, 9, 4, 15)
c = c(2, 4, 1, 8, 4)
d = c(2, 4, 1, 5, 3)
df = data.frame(a, b, c, d)
I want to calcucate the proportion of b, c and d of a and I want a new column for each of the outcomes. My code without functions etc looks like this:
df$c_p = round((df$c / df$a)*100, digits = 2)
df$d_p = round((df$d / df$a)*100, digits = 2)
What's the easiest way to get the same output I do without having to copypaste the code over and over again? My dataframe is much bigger in reality and it's time for me to learn how to do this more efficiently.
Thank you!
athere...