ecdf
is vectorized, you can use
apply(iris[, 1:4], 2, function(c) ecdf(c)(c))
Question
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?
Solution
ecdf
is vectorized, you can use
apply(iris[, 1:4], 2, function(c) ecdf(c)(c))