在r中算术算术

发布于 2025-02-06 07:51:06 字数 1518 浏览 1 评论 0原文

基本算术在R中的IFELSE语句中如何工作?我试图通过在满足条件的另一列中将数字添加到另一列的值来创建一个新的列。

library(zoo)
library(dplyr)

start_date <- as.Date("2021-11-1")
end_date <- as.Date("2022-10-31")

ym <- seq(as.yearmon(start_date), as.yearmon(end_date), 1/12)

df <- data.frame(start = pmax(start_date, as.Date(ym)),
           end = pmin(end_date, as.Date(ym, frac = 1)),
           month = month.name[cycle(ym)],
           year = as.integer(ym),
           stringsAsFactors = FALSE)

df$month_num <- match(df$month, month.name)

我的目标是:如果一个月是在5月到12月(5-&gt; 12)之间的“ +1”年度(制作新专栏“ water_year”)。如果月份是在1月至4月之间,请与新专栏中的“数字”留下“编号”。

df$water_year <- df %>% ifelse(month_num >=5, (year+1), year)

结果:

> print(df)
        start        end     month year month_num  water_year
1  2021-11-01 2021-11-30  November 2021        11  2022
2  2021-12-01 2021-12-31  December 2021        12  2022
3  2022-01-01 2022-01-31   January 2022         1  2022
4  2022-02-01 2022-02-28  February 2022         2  2022
5  2022-03-01 2022-03-31     March 2022         3  2022
6  2022-04-01 2022-04-30     April 2022         4  2022
7  2022-05-01 2022-05-31       May 2022         5  2023
8  2022-06-01 2022-06-30      June 2022         6  2023
9  2022-07-01 2022-07-31      July 2022         7  2023
10 2022-08-01 2022-08-31    August 2022         8  2023
11 2022-09-01 2022-09-30 September 2022         9  2023
12 2022-10-01 2022-10-31   October 2022        10  2023

How does basic arithmetic work inside an ifelse statement in R? I'm trying to create a new column simply by adding a number to another column of values, if the condition is met.

library(zoo)
library(dplyr)

start_date <- as.Date("2021-11-1")
end_date <- as.Date("2022-10-31")

ym <- seq(as.yearmon(start_date), as.yearmon(end_date), 1/12)

df <- data.frame(start = pmax(start_date, as.Date(ym)),
           end = pmin(end_date, as.Date(ym, frac = 1)),
           month = month.name[cycle(ym)],
           year = as.integer(ym),
           stringsAsFactors = FALSE)

df$month_num <- match(df$month, month.name)

My goal: If month is between May to December (5->12) add "+1" to variable year (making new column "water_year"). If month is between Jan. to April, leave year "number" as is in the new column.

df$water_year <- df %>% ifelse(month_num >=5, (year+1), year)

Result:

> print(df)
        start        end     month year month_num  water_year
1  2021-11-01 2021-11-30  November 2021        11  2022
2  2021-12-01 2021-12-31  December 2021        12  2022
3  2022-01-01 2022-01-31   January 2022         1  2022
4  2022-02-01 2022-02-28  February 2022         2  2022
5  2022-03-01 2022-03-31     March 2022         3  2022
6  2022-04-01 2022-04-30     April 2022         4  2022
7  2022-05-01 2022-05-31       May 2022         5  2023
8  2022-06-01 2022-06-30      June 2022         6  2023
9  2022-07-01 2022-07-31      July 2022         7  2023
10 2022-08-01 2022-08-31    August 2022         8  2023
11 2022-09-01 2022-09-30 September 2022         9  2023
12 2022-10-01 2022-10-31   October 2022        10  2023

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文