2

我试图获取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)})

但不知道如何存储结果。

有什么帮助吗?

4

1 回答 1

4

ecdf是矢量化的,你可以使用

apply(iris[, 1:4], 2, function(c) ecdf(c)(c))
于 2013-11-28T09:59:27.270 回答