Displaying 1 result from an estimated 1 matches for "calcpercent".
2010 Nov 30
1
more flexible "ave"
...data.frame(site = c("a", "a", "a", "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), calcPercen...