Home > Enterprise >  Date is converted to numeric when iterating in R
Date is converted to numeric when iterating in R

Time:04-16

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! :)

CodePudding user response:

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

CodePudding user response:

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)

  • Related