R-从colMeans函数创建数据框 [英] R - creating dataframe from colMeans function

查看:84
本文介绍了R-从colMeans函数创建数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图从原始数据帧创建一个数据帧,其中新数据帧中的行代表旧数据帧中每20行的平均值。我发现了一个名为colMeans的函数,该函数可以很好地完成工作,唯一的问题仍然存在,那就是如何将结果向量转换回数据框,以便进一步分析。

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.

我的colMeans代码:(原始数据框中的矩阵1转换为矩阵,这是我设法使其工作的唯一方法)

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));

但是在这里,我得到了数字序列,该序列将所有结果连接在一个单独的列中(如果我尝试例如as.data.frame(a))。我应该如何将结果返回到数据框中,其中每一列仅包含特定列名称的结果,而不是所有平均值。

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.

推荐答案

基于方法('as.data.frame') as.data.frame.list 是将 vector 的每个元素转换为 data.frame

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)

这篇关于R-从colMeans函数创建数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆