Currently I wrote this code:
lc <- round(tabyl(x$Likelihood.to.Click, show_na = FALSE),2)
lc$percent <- lc$percent * 100
and produced this chart:
But In need help manipulating it to create the below df (basically its summing the percentages of row 1 and 2, leaving 3 as is, and then summing 4 and 5:
First make your example reproducible using dput(lc)
to pass the data:
lc <- structure(list(L2E = 1:5, n = c(7, 23, 84, 73, 33), pct = c(3,
10, 38, 33, 15)), class = "data.frame", row.names = c(NA, -5L))
Now define the groups
groups <- list(1:2, 3, 4:5)
sumpct <- sapply(groups, function(x) sum(lc[x, 3]))
lc2 <- data.frame(group=seq(length(groups)), sumpct)
lc2
# group sumpct
# 1 1 13
# 2 2 38
# 3 3 48
Note that row 3 is 38, not 32 and in rows 4 and 5, 33 + 15 = 48, not 54.