【问题标题】:Trying to create a function to calculate over 6 months instead of a year试图创建一个函数来计算超过 6 个月而不是一年
【发布时间】:2019-08-02 19:38:39
【问题描述】:

我在 r 中创建了一个函数,它通过过滤每年进行一些计算。现在我想从每年中过滤掉 6 个月。例如,2014 将分为两个数据帧 20140101-201406 和 20140701-20141201。

我尝试在过滤器命令中使用逻辑运算符,但它不断给我错误作为意外符号。

func2 <- function(years, data) {
years <- c(2014, 2015, 2016, 2017, 2018, 2019)
listofdfs <- list()
efor(i in 1:length(years)) {
#d <- data[data$Year == years[i]]
d <- filter(data, (data$Year==years[i]))
df <- data.frame(d)[, c(4,5)]
names(df) <- unlist(d[1, "headers"])
names(df)[1:2] <- c("Actual", "Estimated")
listofdfs[[i]] <- df
  }
return(listofdfs)
}

预计应该给我每个月有 6 行的数据框,但它给出的是全年

编辑:

这是我累的:

func2 <- function(years, data) { 

  years <- c(20140101, 20140601, 20140701,20141201, 20150101, 20150601, 20150701, 20151201, 20160101,20191201)

  listofdfs <- list() for(i in 1:length(years)) { #d <- data[data$Year == years[i]] 

  d <- filter(data, (years[i]==data$Year || years[i]<data$Year) | data$Year==inc(years)[i] || data$Year<inc(years[i]))

  df <- data.frame(d)[, c(4,5)] 

  names(df) <- unlist(d[1, "headers"]) 
}

This is what the data looks like

【问题讨论】:

  • func2
  • 您可以发布示例数据吗?请使用dput(data) 的输出编辑问题。或者,如果 dput(head(data, 20)) 的输出太大。另外,inc(years[i]) 是什么?代码示例中未定义函数inc()

标签: r function filter


【解决方案1】:

如果您想将数据拆分为学期,这里是一个完全没有循环的基本 R 方法。

fun <- function(data){
  d <- as.Date(data[[1]], format = "%Y%m%d")
  m <- as.integer(format(d, "%m"))
  semester <- 2 - (m <= 6)
  year <- format(d, "%Y")
  split(data, list(semester, year))
}

只需将数据集传递给这个函数,它就会输出一个数据框列表,每学期/年一个。

【讨论】:

    【解决方案2】:

    您的代码中有一些错误!

    在第 2 行中,“2019”之后缺少一个右括号 )。仅此一项就可以解释您的错误。

    但是如果没有样本数据,就很难解决你原来的问题。

    编辑 1:

    真的很难说你的问题是什么,因为你在帖子和评论中添加的代码有太多的语法错误,无法查明问题。

    也许这些语法错误是问题所在,但问题太多了,缺少很多右括号,注释掉的行等。

    如果我以您的代码示例为例,这就是消除这些错误的样子,这能解决您的问题吗?

    func2 <- function(years, data) { 
    
      years <- c(20140101, 20140601, 20140701,20141201, 20150101, 20150601, 20150701, 20151201, 20160101,20191201)
    
      listofdfs <- list() for(i in 1:length(years)) {
    
      #d <- data[data$Year == years[i]] 
    
      d <- filter(data, (years[i]==data$Year || years[i]<data$Year) | data$Year==inc(years)[i] || data$Year<inc(years[i]))
    
      df <- data.frame(d)[, c(4,5)] 
    
      names(df) <- unlist(d[1, "headers"])
      }
    }
    

    编辑 2:

    所以这里有一个解决方案:

    # Sample data that looks like yours
    
    years <- c(20140101, 20140601, 20140701,20141201, 20150101, 20150601, 20150701, 20151201, 20160101)
    test <- c("A", "A","A","B","B","B", "C", "C", "C")
    
    df <- data.frame(years,test)
    
    library(dplyr)
    
    # This code creates a splitting variable, Halfyear
    df %>%
      separate(years,into = c("Year","Month","Day"), sep = c(4,6)) %>%
      mutate(Halfyear = paste(Year,case_when(Month <= 6 ~ "First Half",TRUE ~ "Second Half"))) %>%
      mutate(Halfyear = as.factor(Halfyear)) %>%
      {.} -> df
    
    # Now we can use split to create the relevant data frames and access them with [i] where i is the index of the half year
      as.data.frame(split(df,f = df$Halfyear)[1]) %>%
      head()
    

    让我知道这是否有效。

    【讨论】:

    • 嘿,我在发布问题时在语法上犯了一些错误。我认为现在应该没问题了。编辑 1:只有我尝试过的代码。我还发布了一张数据框的图片,如果有帮助的话?
    • @RohanJain 最终结果应该是每年两个数据帧,每个数据帧每个月包含一行?我想我有一个解决方案,我会在一分钟内编辑。
    • 要我在函数中添加吗?
    • @RohanJain 不,这会替换整个函数,但您可以从中构建一个新函数。
    【解决方案3】:

    考虑 splitby 将数据帧按一个或多个因素(特别是 year 并计算出 year_half)子集到数据帧列表中:

    df$year_half <- ifelse(as.integer(substring(df$Year, 5, 6)) <= 6, 
                           "first_half", "second_half")
    
    # SIMILAR CALLS:
    listofdfs <- split(df, list(df$year, df$year_half))
    listofdfs <- by(df, df[,c("year", "year_half")], FUN=identity)
    

    【讨论】:

    • 你想让我把它包含在函数中吗? func2
    • 您是否运行过此解决方案? splitby 将在数据中的所有 yearsyear_half 中进行子集化。您不需要提供任何东西或初始化任何空列表。请告知上述两种解决方案中的任何一种对您没有帮助。
    • 这给了我栏 year_half 但它充满了 NAs
    • 尝试先转换成字符:...substring(as.character(df$Year), 5, 6)...
    猜你喜欢
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2013-05-23
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    相关资源
    最近更新 更多