一组在多个数据帧上的函数,并将输出合并到R中 [英] A set of functions over multiple data frames and merge the outputs in R

查看:57
本文介绍了一组在多个数据帧上的函数,并将输出合并到R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个数据框(在130个观察点处,不同持续时间的温度变化),并且想要通过将以下代码应用于每个数据框来生成所有数据的月平均值-然后将结果放入一个数据框.我一直在尝试使用for循环执行此操作,但没有成功.我对R还是比较陌生,如果有人可以帮助我解决这个问题,我会非常感激.

I have multiple data frames (moving temperature of different duration at 130 observation points), and want to generate monthly average for all the data by applying the below code to each data frame - then put the outcome into one data frame. I have been trying to do this with for-loop, but not getting anywhere. I'm relatively new to R and really appreciate if someone could help me get through this.

这是数据框的一瞥:

head(maxT2016[,1:5])

      X       X0       X1       X2       X3
1 20160101 26.08987 26.08987 26.08987 26.08987
2 20160102 25.58242 25.58242 25.58242 25.58242
3 20160103 25.44290 25.44290 25.44290 25.44290
4 20160104 26.88043 26.88043 26.88043 26.88043
5 20160105 26.60278 26.60278 26.60278 26.60278
6 20160106 24.87676 24.87676 24.87676 24.87676

str(maxT2016)
'data.frame':   274 obs. of  132 variables:
$ X   : int  20160101 20160102 20160103 20160104 20160105 20160106 20160107 20160108 20160109 20160110 ...

$ X0  : num  26.1 25.6 25.4 26.9 26.6 ...
$ X1  : num  26.1 25.6 25.4 26.9 26.6 ...
$ X2  : num  26.1 25.6 25.4 26.9 26.6 ...
$ X3  : num  26.1 25.6 25.4 26.9 26.6 ...

这是我用于单个数据框的代码:

Here is the code that I use for individual data frame:

library(dplyr)
library(lubridate)
library(tidyverse)

maxT10$X <- as.Date(as.character(maxTsma10$X), format="%Y%m%d") 

monthlyAvr <- maxT10 %>%
  group_by(month=floor_date(date, "month")) %>%
  summarise(across(X0:X130, mean, na.rm=TRUE)) %>%
  slice_tail(n=6) %>%
  select(-month)

monthlyAvr2 <- as.data.frame(t(montlyAvr))
colnames(monthlyAvr2) <- c("meanT_Apr", "meanT_May", "meanT_Jun", "meanT_Jul", "meanT_Aug", 
"meanT_Sep")

基本上,我想将所有所有数据帧放入一个列表中,并在所有数据帧中运行该函数,然后将这些输出排序到一个数据帧中.我遇到了使用lapply函数作为替代方法的情况,但是对于for循环有些满意.

Essentially, I want to put all the all the data frames into a list and run the function through all the data frame, then sort these outputs into one data frame. I came across with lapply function as an alternative, but somewhat felt more comfortable with for-loop.

d = list(maxT10, maxT20, maxT30, maxT60 ... ...)

for (i in 1:lengh(d)){

}

MonthlyAvrT <- cbind(maxT10, maxT20, maxT30, maxT60... ... ) 

推荐答案

罗勒.欢迎使用StackOverflow.

Basil. Welcome to StackOverflow.

当我第一次使用R声明时,我对 lapply 保持警惕,但您应该坚持使用它.它几乎总是比使用for循环更有效.在特定情况下,您可以将各个数据框放在 list 中,然后将在每个数据框上运行的代码放入函数 myFunc 中,例如,该函数获取所需的数据框以其论点进行处理.

I was wary of lapply when I first stated using R, but you should stick with it. It's almost always more efficient than using a for loop. In your particular case, you can put your individual data frames in a list and the code you run on each into a function myFunc, say, which takes the data frame you want to process as its argument.

那你可以简单地说

allData <- bind_rows(lapply(1:length(dataFrameList), function(x) myFunc(dataFrameList[[x]])))

顺便说一句,您的列名使我认为您的数据尚未

Incidentally, your column names make me think your data isn't yet tidy. I'd suggest you spend a little time making it so before you do much else. It will save you a huge amount of effort in the long run.

这篇关于一组在多个数据帧上的函数,并将输出合并到R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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