for loop - Creating a cumulative distribution within a column of a matrix in R -
i have situation there c probability distributions. each distribution has r possiblities. can model (r x c) matrix c'th column represents c'th probability distribution. element @ [r,c] represents probability of getting r'th possibility c'th probability distribution. name matrix probdist.
i want generate cumulative probability distribution matrix, each element @ [r,c] in matrix sum of elements [1,c] [r-1,c].
i coding in r. currently, code follows:
cumprobdist <- probdist for(c in 1:ncol(probdist)) cumprobdist[c] <- cumsum(probdist[c])
which works correctly, aware function frowned upon in r code due being inefficient. how may rewrite chunk of code utilize *apply family of functions (or efficient) instead?
cumprobdist = apply(probdist,2,cumsum)
Comments
Post a Comment