从R中的数据帧列表中的意思 [英] Means from a list of data frames in R

查看:112
本文介绍了从R中的数据帧列表中的意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R比较新,有一个复杂的情况来解决。我已经将超过1000个数据帧的列表上传到R中,并将其列为x。我想做的是采取某些数据帧,并采取整个数据帧的均值和方差(不包括每个数据帧的第一列),并将其保存为两个单独的向量。例如,我希望从元素(3)开始,从元素(3)开始,从元素(54)中取出列表中每个第三个数据框的均值和方差。



最终需要两个向量:

  meanvector = c(mean(data frame(3)),mean(data frame(6) ),...,mean(数据帧(54)))
variancevector = c(var(data frame(3)),var(data frame(6)),...,var(data frame 54)))

这个问题超出了我的知识水平,但我想我可以做到这一点使用某种循环,但我不知道如何做这样的循环。任何帮助将不胜感激!谢谢你提前。

解决方案

您可以使用 lapply 并传递索引如下:

  ids<  -  seq(3,54,by = 3)
out< - do .call(rbind,lapply(ids,function(idx){
t < - unlist(x [[idx]] [,-1])$ ​​b $ bc(mean(t),var(t)
}))


I am relatively new to R and have a complicated situation to solve. I have uploaded a list of over 1000 data frames into R and called this list x. What I want to do is take certain data frames and take the mean and variance of the entire data frames (excluding the first column of each) and save these into two separate vectors. For example I wish to take the mean and variance of every third data frame in the list starting from element (3) and going to element (54).

So what I ultimately want are two vectors:

meanvector=c(mean(data frame(3)), mean(data frame(6)),..., mean(data frame(54)))
variancevector=c(var(data frame (3)), var(data frame (6)), ..., var(data frame(54)))

This problem is way above my knowledge level but I am thinking I can do this effectively using some sort of loop but I do not know how to go about making such loop. Any help would be much appreciated! Thank you in advance.

解决方案

You can use lapply and pass indices as follows:

ids <- seq(3, 54, by=3)
out <- do.call(rbind, lapply(ids, function(idx) {
    t <- unlist(x[[idx]][, -1])
    c(mean(t), var(t))
}))

这篇关于从R中的数据帧列表中的意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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