6
我必须找出连续变量按因子计算的累计频率,转换为百分比。 例如:按因子计算的累计频率
data <- data.frame(n = sample(1:12),
d = seq(10, 120, by = 10),
Site = rep(c("FirstSite", "SecondSite"), 6),
Plot = rep(c("Plot1", "Plot1", "Plot2", "Plot2"), 3)
)
data <- with(data, data[order(Site,Plot),])
data <- transform(data, G = ((pi * (d/2)^2) * n)/10000)
data
n d Site Plot G
1 7 10 FirstSite Plot1 0.05497787
5 9 50 FirstSite Plot1 1.76714587
9 12 90 FirstSite Plot1 7.63407015
3 10 30 FirstSite Plot2 0.70685835
7 5 70 FirstSite Plot2 1.92422550
11 1 110 FirstSite Plot2 0.95033178
2 3 20 SecondSite Plot1 0.09424778
6 8 60 SecondSite Plot1 2.26194671
10 6 100 SecondSite Plot1 4.71238898
4 4 40 SecondSite Plot2 0.50265482
8 2 80 SecondSite Plot2 1.00530965
12 11 120 SecondSite Plot2 12.44070691
我需要因素G
列的cumulaive频率Plot~Site
为了绘制的G
一个geom_step ggplot针对d
每个情节和站点。
我已经实现通过因子通过以计算G
累计总和:
data.ss <- by(data[, "G"], data[,c("Plot", "Site")], function(x) cumsum(x))
# Gtot
(data.ss.tot <- sapply(ss, max))
[1] 9.456194 3.581416 7.068583 13.948671
现在我需要表达在其中1是G
TOT为每个Plot
范围[0..1]的每个Plot
G
。我想我应该划分G
其Plot
Gtot
,然后应用一个新的cumsum
它。怎么做?
请注意,我必须将此累积频率与d
而不是G
本身进行比较,所以它不是一个合适的ecdf。
谢谢。
非常优雅的解决方案,谢谢。 – mbask 2012-01-17 04:28:44