Displaying 1 result from an estimated 1 matches for "pct_val1".
2010 Nov 30
1
more flexible "ave"
...t;, "b", "b", "b"),
gr = c("total", "x1", "x2", "x1", "total","x2"),
value1 = c(212, 56, 87, 33, 456, 213))
df
calcPercent <- function(df) {
df <- transform(df, pct_val1 = ave(df[, -c(1:2)], df$gr,
FUN = function(x)
x/df[df$gr == "total", "value1"]) )
}
# This works as intended...
w <- lapply(split(df, df$site), calcPercent)
w <- do.call(rbind, w)
w
# ... but when I add a new...