2

I am trying to get the ecdf() of each column c in a data frame and then feed the rows r of a column into that column's ecdf(c) to return the corresponding value ecdf(c)(r) per cell. The function below works.

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)

However, I am 99.5% sure there is a one liner code that includes apply to do this. I tried:

apply(iris, 2, function(x) for (r in x){ ecdf(x)(r)})

but don't know how to store the results.

Any help?

Zhubarb
  • 11,432
  • 18
  • 75
  • 114

1 Answers1

4

ecdf is vectorized, you can use

apply(iris[, 1:4], 2, function(c) ecdf(c)(c))
rcs
  • 67,191
  • 22
  • 172
  • 153
  • `ecdf(c)` returns a (step)function which accepts a vector as argument for evaluation (evaluated at positions `c` again in this example) – rcs Nov 28 '13 at 10:30
  • Yes, stupid of me. I have actually fed vectors into `ecdf()` before but for some reason it did not occur to me in this case... – Zhubarb Nov 28 '13 at 10:42