r语言 - 如何正确地基于一个特定的日期列排名和行?



这个想法是根据列的名称来获得总和01/01/202101/08/2021之间:

# define rank parameters {start-end}
first_date <- format(Sys.Date(), "01/01/%Y")
actual_date <- format(Sys.Date() %m-% months(1), "01/%m/%Y")

# get the sum of the rows between first_date and actual_date
df$ytd<- rowSums(df[as.character(seq(first_date,
actual_date))])

但是,当应用下一个错误时出现:

seq.default(first_date, to_date)错误:'from'必须是一个有限数

期望输出是一个新列,取指定秩之间的行和。

数据
df <- structure(list(country = c("Mexico", "Mexico", "Mexico", "Mexico"
), `01/01/2021` = c(12, 23, 13, 12), `01/02/2021` = c(12, 23, 
13, 12), `01/03/2021` = c(12, 23, 13, 12), `01/04/2021` = c(12, 
23, 13, 12), `01/05/2021` = c(12, 23, 13, 12), `01/06/2021` = c(12, 
23, 13, 12), `01/07/2021` = c(12, 23, 13, 12), `01/08/2021` = c(12, 
23, 13, 12), `01/09/2021` = c(12, 23, 13, 12), `01/10/2021` = c(12, 
23, 13, 12), `01/11/2021` = c(12, 23, 13, 12), `01/12/2021` = c(12, 
23, 13, 12)), row.names = c(NA, -4L), class = c("tbl_df", "tbl", 
"data.frame"))

我怎样才能正确地应用一个函数来得到这个输出?

formatseq不工作,即seq期望Date类,而formatcharacter类。相反,使用acrossselect

中的范围操作符。
library(dplyr)
out <- df %>% 
mutate(ytd = rowSums(across(all_of(first_date):all_of(actual_date)))) 

与产出

> out$ytd
[1]  96 184 104  96

使用match-

的base R方法
df$ytd <- rowSums(df[match(first_date, names(df)):match(actual_date, names(df))])
df$ytd
#[1]  96 184 104  96

相关内容

  • 没有找到相关文章

最新更新