dataframe - How to save the result of R apply functions into a matrix? -
suppose have data set x, has dimension 100 x 10. want apply set of functions each column, , save results in new data frame same column headers.
example
m <- matrix(rexp(200, rate=.1), ncol=20) m_df <- as.data.frame(m) row1 <- apply(m_df,2,mean) row2 <- apply(m_df,2,var) row3 <- apply(m_df,2,sum) i want make new matrix comprised of row1, row2, , row3 has same column headers m_df.
thanks! max
try this:
apply(m_df,2,function(x) c(mean = mean(x),var = var(x),sum = sum(x))) although, keep in mind first thing apply convert m_df matrix, there's not point in converting data frame before have to.
Comments
Post a Comment