在R中迭代时,日期转换为数字

发布于 2025-01-21 10:44:41 字数 491 浏览 0 评论 0 原文

我有一个数据框“热浪”,其中一个名为“日期”的列是该类是日期的列,

> class(HeatWave$Date)
[1] "Date"

我想通过这些日期进行迭代并检索每个月的一个月,

for (i in HeatWave$Date){
   month <- format(i, '%m')
}

但是此触发了错误,

Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L,  : 
invalid 'trim' argument

似乎是迭代的 错误通过这些日期,I 变量使其可以将类别更改为数字,就像

> class(i)
[1] "numeric"

我该如何使其正常工作一样?谢谢! :)

I have a data frame "HeatWave" with a column named "Date" whose class is Date, as checked through

> class(HeatWave$Date)
[1] "Date"

I want to iterate through those dates and retrieve the month of each one

for (i in HeatWave$Date){
   month <- format(i, '%m')
}

But this triggers the error

Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L,  : 
invalid 'trim' argument

It seems that iterating the i variable through those dates makes it change the class to numeric, as visible with

> class(i)
[1] "numeric"

how can I make it work? Thanks! :)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤舞 2025-01-28 10:44:41

您的循环的问题

是将 date 转换为数字值。这导致格式()使用数字值的方法,其中 trim 是第一个参数:

for (i in as.Date("2022-04-15")) print(i)
#> [1] 19097


format(19097, "%m")
#> Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : invalid 'trim' argument

在2022-04--创建15 by reprex package (v2.0.1)

loop解决方案

通过数据框架的索引循环循环而不是实际值。

months <- numeric(nrow(HeatWave))

for (i in seq_along(HeatWave$Date)) {
  
  months[i] <- format(HeatWave$Date[i], '%m')
  
}

months
#>  [1] "03" "03" "03" "03" "03" "03" "03" "03" "03" "03"

创建整理解决方案

使用 dplyr :: mutate() lubridate :: month()更容易做到这一点:

library(dplyr)

HeatWave %>% 
  mutate(
    month = lubridate::month(Date)
  )
#>          Date month
#> 1  2022-03-31     3
#> 2  2022-03-30     3
#> 3  2022-03-29     3
#> 4  2022-03-28     3
#> 5  2022-03-27     3
#> 6  2022-03-26     3
#> 7  2022-03-25     3
#> 8  2022-03-24     3
#> 9  2022-03-23     3
#> 10 2022-03-22     3

在2022-04-15创建的 reprex软件包(v2.0.1)

示例数据

HeatWave <- data.frame(
  Date = as.Date("2022-04-01") - 1:10
)

HeatWave
#>          Date
#> 1  2022-03-31
#> 2  2022-03-30
#> 3  2022-03-29
#> 4  2022-03-28
#> 5  2022-03-27
#> 6  2022-03-26
#> 7  2022-03-25
#> 8  2022-03-24
#> 9  2022-03-23
#> 10 2022-03-22

The problem

Your loop is converting the Date into a numeric value. This causes format() to use the method for numeric values, where trim is the first argument:

for (i in as.Date("2022-04-15")) print(i)
#> [1] 19097


format(19097, "%m")
#> Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : invalid 'trim' argument

Created on 2022-04-15 by the reprex package (v2.0.1)

A loop solution

Loop through the index of the dataframe instead of the actual values.

months <- numeric(nrow(HeatWave))

for (i in seq_along(HeatWave$Date)) {
  
  months[i] <- format(HeatWave$Date[i], '%m')
  
}

months
#>  [1] "03" "03" "03" "03" "03" "03" "03" "03" "03" "03"

Created on 2022-04-15 by the reprex package (v2.0.1)

A tidy solution

Use dplyr::mutate() and lubridate::month() to do this even easier:

library(dplyr)

HeatWave %>% 
  mutate(
    month = lubridate::month(Date)
  )
#>          Date month
#> 1  2022-03-31     3
#> 2  2022-03-30     3
#> 3  2022-03-29     3
#> 4  2022-03-28     3
#> 5  2022-03-27     3
#> 6  2022-03-26     3
#> 7  2022-03-25     3
#> 8  2022-03-24     3
#> 9  2022-03-23     3
#> 10 2022-03-22     3

Created on 2022-04-15 by the reprex package (v2.0.1)

The example data

HeatWave <- data.frame(
  Date = as.Date("2022-04-01") - 1:10
)

HeatWave
#>          Date
#> 1  2022-03-31
#> 2  2022-03-30
#> 3  2022-03-29
#> 4  2022-03-28
#> 5  2022-03-27
#> 6  2022-03-26
#> 7  2022-03-25
#> 8  2022-03-24
#> 9  2022-03-23
#> 10 2022-03-22

杀手六號 2025-01-28 10:44:41

这是 sapply 方式。
下面的第一个 sapply 显示循环获得日期,但打印语句输出数字。使用 sapply 格式语句按需运行。

HeatWave <- data.frame(Date = Sys.Date() - 30:0)

sapply(HeatWave$Date, \(i) print(i))
#> [1] "2022-03-16"
#> [1] "2022-03-17"
#> [1] "2022-03-18"
#> [1] "2022-03-19"
#> [1] "2022-03-20"
#> [1] "2022-03-21"
#> [1] "2022-03-22"
#> [1] "2022-03-23"
#> [1] "2022-03-24"
#> [1] "2022-03-25"
#> [1] "2022-03-26"
#> [1] "2022-03-27"
#> [1] "2022-03-28"
#> [1] "2022-03-29"
#> [1] "2022-03-30"
#> [1] "2022-03-31"
#> [1] "2022-04-01"
#> [1] "2022-04-02"
#> [1] "2022-04-03"
#> [1] "2022-04-04"
#> [1] "2022-04-05"
#> [1] "2022-04-06"
#> [1] "2022-04-07"
#> [1] "2022-04-08"
#> [1] "2022-04-09"
#> [1] "2022-04-10"
#> [1] "2022-04-11"
#> [1] "2022-04-12"
#> [1] "2022-04-13"
#> [1] "2022-04-14"
#> [1] "2022-04-15"
#>  [1] 19067 19068 19069 19070 19071 19072 19073 19074 19075 19076 19077 19078
#> [13] 19079 19080 19081 19082 19083 19084 19085 19086 19087 19088 19089 19090
#> [25] 19091 19092 19093 19094 19095 19096 19097

month <- sapply(HeatWave$Date, \(i) format(i, "%m"))
month
#>  [1] "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03"
#> [16] "03" "04" "04" "04" "04" "04" "04" "04" "04" "04" "04" "04" "04" "04" "04"
#> [31] "04"

Here is a sapply way.
The first sapply below shows that the loop gets a date but the print statement outputs numbers. With sapply, the format statement works as wanted.

HeatWave <- data.frame(Date = Sys.Date() - 30:0)

sapply(HeatWave$Date, \(i) print(i))
#> [1] "2022-03-16"
#> [1] "2022-03-17"
#> [1] "2022-03-18"
#> [1] "2022-03-19"
#> [1] "2022-03-20"
#> [1] "2022-03-21"
#> [1] "2022-03-22"
#> [1] "2022-03-23"
#> [1] "2022-03-24"
#> [1] "2022-03-25"
#> [1] "2022-03-26"
#> [1] "2022-03-27"
#> [1] "2022-03-28"
#> [1] "2022-03-29"
#> [1] "2022-03-30"
#> [1] "2022-03-31"
#> [1] "2022-04-01"
#> [1] "2022-04-02"
#> [1] "2022-04-03"
#> [1] "2022-04-04"
#> [1] "2022-04-05"
#> [1] "2022-04-06"
#> [1] "2022-04-07"
#> [1] "2022-04-08"
#> [1] "2022-04-09"
#> [1] "2022-04-10"
#> [1] "2022-04-11"
#> [1] "2022-04-12"
#> [1] "2022-04-13"
#> [1] "2022-04-14"
#> [1] "2022-04-15"
#>  [1] 19067 19068 19069 19070 19071 19072 19073 19074 19075 19076 19077 19078
#> [13] 19079 19080 19081 19082 19083 19084 19085 19086 19087 19088 19089 19090
#> [25] 19091 19092 19093 19094 19095 19096 19097

month <- sapply(HeatWave$Date, \(i) format(i, "%m"))
month
#>  [1] "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03"
#> [16] "03" "04" "04" "04" "04" "04" "04" "04" "04" "04" "04" "04" "04" "04" "04"
#> [31] "04"

Created on 2022-04-15 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文