使用for循环对多个数据框应用更改(分组依据) [英] Apply changes (group by) on multiple dataframes using for loop

查看:143
本文介绍了使用for循环对多个数据框应用更改(分组依据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大约有33个数据帧(df1,df2,df3,df4 ...),如下所示:

I have around 33 dataframes (df1, df2, df3, df4 ...) that look like this:

Date       Month   Value  
2018-07-16 2018-07     10 
2018-07-17 2018-07     2
2018-07-18 2018-07     4 
2018-07-19 2018-07     45
2018-07-20 2018-07     13

,我想按月对每个数据帧进行分组,如下所示:

and I would like to group each data frame by month, like this:

df1 = df1 %>% group_by(Month)%>%
  summarise(
    sd_value = sd(value)
  )

如何在所有数据帧上做到这一点而不重复? 另外,我需要将结果导出为单独的数据框.

How can I do this on all dataframes without repeating it? Also, I will need to export the results as separate data frames.

我尝试使用for循环复制其他人的解决方案,但不起作用. 另外,我在我的环境中单独拥有所有数据框,它们不在列表中.

I've tried to duplicate some other people's solutions using for loop but doesn't work. Also, I have all the dataframes separately in my Environment, they are not in a list.

推荐答案

您可以使用带有样式的mget将它们放在列表中,然后依次使用lapplyaggregate

You can get them in list using mget with your pattern, loop over them using lapply and then aggregate

list_name <- ls(pattern = "df\\d+")

list_df <- lapply(mget(list_name), function(x) aggregate(Value~Month, x, sd))
list_df

#$df1
#    Month    Value
#1 2018-07 17.45566

#$df2
#    Month    Value
#1 2018-07 185.8744

或者如果您想使用tidyverse

library(tidyverse)

list_df <- map(mget(list_name), 
          . %>% group_by(Month) %>% summarise(sd_value = sd(Value)))

要在单独的csv中编写它们,我们可以使用mapply

To write them in separate csv's we can use mapply

mapply(function(x, y) write.csv(x, 
      paste0("path/to/file/", y, ".csv"), row.names = FALSE), list_df, list_name)

数据

df1 <- structure(list(Date = structure(1:5, .Label = c("2018-07-16", 
"2018-07-17", "2018-07-18", "2018-07-19", "2018-07-20"), class = "factor"), 
Month = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "2018-07", class = "factor"), 
Value = c(10L, 2L, 4L, 45L, 13L)), class = "data.frame", row.names = 
c(NA, -5L))

df2 <- structure(list(Date = structure(1:5, .Label = c("2018-07-16", 
 "2018-07-17", "2018-07-18", "2018-07-19", "2018-07-20"), class = "factor"), 
Month = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "2018-07", class = "factor"), 
Value = c(11L, 2L, 4L, 423L, 13L)), class = "data.frame", row.names = 
c(NA, -5L))

这篇关于使用for循环对多个数据框应用更改(分组依据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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