Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - creating dataframe from colMeans function

Tags:

dataframe

r

I've been trying to create a dataframe from my original dataframe, where rows in the new dataframe would represent mean of every 20 rows of the old dataframe. I discovered a function called colMeans, which does the job pretty well, the only problem, which still persists is how to change that vector of results back to dataframe, which can be further analysed.

my code for colMeans: (matrix1 in my original dataframe converted to matrix, this was the only way I managed to get it to work)

a<-colMeans(matrix(matrix1, nrow=20));

But here I get the numeric sequence, which has all the results concatenated in one single column(if I try for example as.data.frame(a)). How am I supposed to get this result back into dataframe where each column includes only the results for specific column name and not all the averages.

I hope my question is clear, thanks for help.

like image 690
sdgaw erzswer Avatar asked Sep 21 '15 09:09

sdgaw erzswer


1 Answers

Based on the methods('as.data.frame'), as.data.frame.list is an option to convert each element of a vector to columns of a data.frame

as.data.frame.list(a)

data

m1 <- matrix(1:20, ncol=4, dimnames=list(NULL, paste0('V', 1:4)))
a <- colMeans(m1)
like image 158
akrun Avatar answered Sep 19 '22 12:09

akrun