从数据帧的数据帧在for循环中创建不同的数据

从数据帧的数据帧在for循环中创建不同的数据,r,group-by,dplyr,R,Group By,Dplyr,我有一个数据帧(称为datasTX)的数据帧: 我甚至不知道如何重现(datasTX),但在datasTX中,数据帧是这样构建的: data <- as.data.frame(seq(as.Date('2017/04/01'), as.Date('2018/11/01'), by="day")) data <- rlang::set_names(data, "Date") data[, "Year"] <- format(data[,"Date"], "%Y") data[,

我有一个数据帧(称为datasTX)的数据帧:

我甚至不知道如何重现(datasTX),但在datasTX中,数据帧是这样构建的:

data <- as.data.frame(seq(as.Date('2017/04/01'), as.Date('2018/11/01'), by="day"))
data <- rlang::set_names(data, "Date")
data[, "Year"] <- format(data[,"Date"], "%Y")
data[, "Month"] <- format(data[,"Date"], "%m")
data[, "Quantity"] <- sample(100, size = nrow(data), replace = TRUE)

data您可以通过使用带有dplyr的管道,对数据进行分组,然后对其进行汇总,来自动完成此操作

在一个数据帧的情况下
库(dplyr)
日期=序号(截止日期('2017/04/01')、截止日期('2018/11/01'),by=“天”)
数据%
分组单位(年、月)%>%
总结(每月=总和(数量))
##tibble:20 x 3
##组别:年份[2]
#年-月-月
#     
#   2017  04       1431
#   2017  05       1511
#   2017  06       1819
#   2017  07       1242
#   2017  08       1583
#   2017  09       1385
#   2017  10       1269
#   2017  11       1572
#   2017  12       1132
在数据帧列表的情况下 在这里,您可以使用purrr(相当于循环的映射函数),并为其提供与我们所做的相同的步骤,映射中的每个循环返回一个数据帧,然后将输出连接起来,结果成为一个数据帧列表

library(dplyr)
library(purrr)

Date = seq(as.Date('2017/04/01'), as.Date('2018/11/01'), by="day")

data <- 
  data.frame(
    Date,
    Year = format(Date, "%Y"),
    Month = format(Date, "%m"),
    Quantity = sample(100, size = length(Date), replace = TRUE))

datasTX <- list(data, data, data)

1:length(datasTX) %>%
  map(function(x){
    datasTX[[x]] %>%
      group_by(Year, Month) %>%
      summarise(monthly = sum(Quantity))
    })
库(dplyr)
图书馆(purrr)
日期=序号(截止日期('2017/04/01')、截止日期('2018/11/01'),by=“天”)
数据%
分组单位(年、月)%>%
总结(每月=总和(数量))
})

是的,我知道我可以一次完成一个数据帧,但我有500多个数据集在一个列表中,我需要在其中完成这项工作。所以我认为循环会更好。除非有不同的方式?您希望输出的格式是什么?数据帧列表?请原谅,我对r还是新手,所以我甚至不知道有哪些选项?数据帧列表似乎很自然,所以我会说是的?是的。您可以将其作为数据帧列表,也可以将所有数据帧附加到一个数据帧中。我已经编辑了这篇文章,现在你可以这样做,如果你想把它变成一个单独的数据帧而不是使用map,那么输出将是一个数据帧列表,你只需用map_df替换它。如果我想为每个数据帧包含一个名称(当前命名为1,2,3等),我是否在函数中包含名称(datasTX)?实际上,我真的需要理解这个for循环。似乎出问题的地方是“每月”
library(dplyr)

Date = seq(as.Date('2017/04/01'), as.Date('2018/11/01'), by="day")

data <- 
  data.frame(
    Date,
    Year = format(Date, "%Y"),
    Month = format(Date, "%m"),
    Quantity = sample(100, size = length(Date), replace = TRUE))

data %>%
  group_by(Year, Month) %>%
  summarise(monthly = sum(Quantity))

# # A tibble: 20 x 3
# # Groups:   Year [2]
# Year  Month monthly
# <fct> <fct>   <int>
#   2017  04       1431
#   2017  05       1511
#   2017  06       1819
#   2017  07       1242
#   2017  08       1583
#   2017  09       1385
#   2017  10       1269
#   2017  11       1572
#   2017  12       1132
library(dplyr)
library(purrr)

Date = seq(as.Date('2017/04/01'), as.Date('2018/11/01'), by="day")

data <- 
  data.frame(
    Date,
    Year = format(Date, "%Y"),
    Month = format(Date, "%m"),
    Quantity = sample(100, size = length(Date), replace = TRUE))

datasTX <- list(data, data, data)

1:length(datasTX) %>%
  map(function(x){
    datasTX[[x]] %>%
      group_by(Year, Month) %>%
      summarise(monthly = sum(Quantity))
    })