0
M數據看起來像這樣:如何使用ddply將函數應用於數據框另一列中由每個因子分隔的一列?
標籤:1 1 1 2 3 2 5 5 5 2 2 3 3 5 6 7 8 ...
編號:132 123 838 29 1 23 0 283 238 123 2 2 ...
這兩列都是數字,我想爲Label中的每個因子計算Numbers的分位數。
#the function I want to use to calc the quantiles
qfn <- function(x) quantile(x, probs = seq(0, 1, 0.2), na.rm = TRUE)
#Using the by function
results <- by(data$Numbers, data$Label, qfn)
我得到正確的結果,但它是一個「by」類而不是數據框。
Label: 1
0% 20% 40% 60% 80% 100%
1.2 3.5 7.8 9.10 30.1 105.3
Label: 2
0% 20% 40% 60% 80% 100%
1.9 2.5 5.8 8.10 23.1 99.3
...
如何使用ddply得到這些相同的結果,但在數據幀?
當我使用類似:
results <- ddply(data, "Label", qfn)
我通過標籤的因素得到正確的分組,但功能被應用到錯誤的列在我的情況 - 它也適用於標籤的值當我想將該功能應用於Numbers時。
謝謝!