我试图获取c
数据框中每一列的 ecdf(),然后将r
一列的行输入该列以返回每个单元格ecdf(c)
的相应值。ecdf(c)(r)
下面的功能有效。
getecdf <- function(data){
# initialise result data
result <- data.frame(matrix(NA, nrow = nrow(data), ncol = ncol(data)));
rownames(result) <- rownames(data); colnames(result) <- colnames(data);
for (c in 1:ncol(data)){
col <- data[,c];
cum <- ecdf(col)
for (r in 1: length(col)){
result[r,c] <- cum(col[r])
}
}
return <- result
}
cum_matrix <- getecdf(iris)
但是,我有 99.5% 的把握有一个单行代码包含apply
执行此操作。我试过了:
apply(iris, 2, function(x) for (r in x){ ecdf(x)(r)})
但不知道如何存储结果。
有什么帮助吗?