buhtz buhtz - 3 months ago 8
R Question

R-like way to add a % column to a data frame

I want to add a column that shows me the percentage part of that row compared to the some of the column. (sorry for my bad mathematical english here).

> trees['Heigth_%'] <- round((100 / sum(trees$Height) * trees$Height), digits=2)
> head(trees)
Girth Height Volume Heigth_%
1 8.3 70 10.3 2.97
2 8.6 65 10.3 2.76
3 8.8 63 10.2 2.67
4 10.5 72 16.4 3.06
5 10.7 81 18.8 3.44
6 10.8 83 19.7 3.52


This work.
But the question is if this is a good and R-like way?

e.g. Is
sum()
called for each row? Or is R intelligent enough here?

Answer

To answer you question if sum is called for every row or is R intelligent enough, you can use trace:

df = data.frame(a = 1:10, b = 21:30)
df['b_%'] = round((100 / sum(df$b) * df$b), digits=2)

trace('sum')
round((100 / sum(df$b) * df$b), digits=2)
untrace('sum')

Which shows only one call to the sum function. Afterwards, R recognizes that the lengths of trees$Height and sum(trees$Height) differ and tries to replicate the shorter one until is has the same length as the bigger one.

Comments