从给定的数据帧中,我try 提取quarterly_values,但是当我try 每三个月改变一次值时,它失败了.

mont_dates <- c("201401", "201402", "201403", "201404", "201405", 
"201406", "201407", "201408", "201409", "201410", "201411", "201412")
cat <- c("new", "old", "undefined", "new", "old", "undefined", 
"undefined", "new", "old", "undefined", "new", "old")
mont_vals <- c(221, 433, 878, 455, 998, 797, 77, 3435, 868, 686, 868, 434)
ID <- c(1,2,3,4,5,6,7,8,9,10,11,12)
df <- data.frame(mont_dates, mont_vals, cat, ID)

函数来提取月值并计算季值

 
Monthly_Qrt_vals <- function(df1) {
  df1 %>%
 mutate(mont_dates = ymd(paste0(mont_dates, "01")),
           quarter = paste0(year(mont_dates), " Q", quarter(mont_dates))) %>%
    arrange(mont_dates) %>%
    mutate(quarterly_values = mont_vals[c(1,seq(3, nrow(mont_vals) , by=3)),])
}
result <- df %>% Monthly_Qrt_vals()
View(result)
         cat ID mont_dates mont_vals quarter
1        new  1 2014-01-01       221 2014 Q1
2        old  2 2014-02-01       433 2014 Q1
3  undefined  3 2014-03-01       878 2014 Q1
4        new  4 2014-04-01       455 2014 Q2
5        old  5 2014-05-01       998 2014 Q2
6  undefined  6 2014-06-01       797 2014 Q2
7  undefined  7 2014-07-01        77 2014 Q3
8        new  8 2014-08-01      3435 2014 Q3
9        old  9 2014-09-01       868 2014 Q3
10 undefined 10 2014-10-01       686 2014 Q4
11       new 11 2014-11-01       868 2014 Q4
12       old 12 2014-12-01       434 2014 Q4

Error

Error in `mutate()`:
ℹ In argument: `quarterly_values = mont_vals[c(1, seq(3, nrow(mont_vals), by = 3)), ]`.
Caused by error in `seq.default()`:
! 'to' must be of length 1

Expected Outcome
quarterly_values should take values of every third month assign to first row of quarters i.e. 2014 Q1, 2014 Q2.

原始数据集是从2014-2023年开始的,有没有替代的解决方案来获得quarterly_values

         cat ID mont_dates mont_vals quarter  quarterly_values 
1        new  1 2014-01-01       221 2014 Q1   878
2        old  2 2014-02-01       433 2014 Q1   NA
3  undefined  3 2014-03-01       878 2014 Q1   NA
4        new  4 2014-04-01       455 2014 Q2   797
5        old  5 2014-05-01       998 2014 Q2   NA
6  undefined  6 2014-06-01       797 2014 Q2   NA
7  undefined  7 2014-07-01        77 2014 Q3   77
8        new  8 2014-08-01      3435 2014 Q3   NA
9        old  9 2014-09-01       868 2014 Q3   NA
10 undefined 10 2014-10-01       686 2014 Q4   686
11       new 11 2014-11-01       868 2014 Q4   NA
12       old 12 2014-12-01       434 2014 Q4   NA

推荐答案

它按季度分组,然后通过将每个季度的第一行设置为等于该季度的最后一个月的值来生成quarterly_values.它使用ifelse来决定是否填充该行,使用tail来 Select 每个组(Quarter)中的最终值.

注意:使用tail假设条目按日期顺序排列.如有必要,您可以先对它们进行分类.

df |> 
   mutate(mont_dates = ymd(paste0(mont_dates, "01")),
          quarter = paste0(year(mont_dates), " Q", quarter(mont_dates))) |>
   group_by(quarter) |>
   mutate(quarterly_values=ifelse(row_number()==1 , tail(mont_vals,1) , NA))


# A tibble: 12 × 6
# Groups:   quarter [4]
   mont_dates mont_vals cat          ID quarter quarterly_values
   <date>         <dbl> <chr>     <dbl> <chr>              <dbl>
 1 2014-01-01       221 new           1 2014 Q1              878
 2 2014-02-01       433 old           2 2014 Q1               NA
 3 2014-03-01       878 undefined     3 2014 Q1               NA
 4 2014-04-01       455 new           4 2014 Q2              797
 5 2014-05-01       998 old           5 2014 Q2               NA
 6 2014-06-01       797 undefined     6 2014 Q2               NA
 7 2014-07-01        77 undefined     7 2014 Q3              868
 8 2014-08-01      3435 new           8 2014 Q3               NA
 9 2014-09-01       868 old           9 2014 Q3               NA
10 2014-10-01       686 undefined    10 2014 Q4              434
11 2014-11-01       868 new          11 2014 Q4               NA
12 2014-12-01       434 old          12 2014 Q4               NA

R相关问答推荐

ggplot 2中的地块底图(basemaps_gglayer()不起作用)

如何删除R中除某些特定名称外的所有字符串?

R Tidymodels textercipes-使用spacyR进行标记化-如何从生成的标记列表中删除标点符号

如何在编辑列时更新可编辑数据表,并使用该表在Shiny中执行连续计算

在rpart. plot或fancyRpartPlot中使用带有下标的希腊字母作为标签?

从一个列表的框架中移除列表包装器

如何删除最后一个可操作对象

绘制采样开始和采样结束之间的事件

R+reprex:在呈现R标记文件时创建可重现的示例

使用不同的定性属性定制主成分分析中点的 colored颜色 和形状

如何删除设置大小的曲线图并添加条形图顶部数字的百分比

在R中的数据框上使用Apply()函数时,如何保留非数字列?

创建在文本字符串中发现两个不同关键字的实例的数据框

为R中的16组参数生成10000个样本的有效方法是什么?

SHILINY中DT列的条件着色

对R中的列表列执行ROW Mean操作

R将函数参数传递给ggploy

Data.table::Shift type=允许扩展数据(&Q;LAG&Q;)

网络抓取NBA.com

将R中对象的CSV数组转换为JSON数组