Home > Net >  How to format the date in r
How to format the date in r

Time:08-05

I have a date format like this:

 head(ergo_bike)
# A tibble: 6 × 8
   hour date...2            time_bike distance calories   power `Participant Code` date...8           
  <dbl> <dttm>                  <dbl>    <dbl>    <dbl>   <dbl> <chr>              <dttm>             
1    12 2022-04-12 00:00:00         2    0         0    0.00613 AE1_01             2022-04-12 00:00:00
2    13 2022-04-12 00:00:00         2    0         0    0.00580 AE1_01             2022-04-12 00:00:00
3    14 2022-04-12 00:00:00         1    0         0    0.00258 AE1_01             2022-04-12 00:00:00
4    14 2022-04-13 00:00:00         2    0         0    0.00714 AE1_01             2022-04-13 00:00:00
5    14 2022-03-11 00:00:00         3    0.746    11.2  0.00868 AE1_02             2022-03-11 00:00:00
6    15 2022-03-11 00:00:00         1    0.250     3.75 0.00274 AE1_02             2022-03-11 00:00:00
structure(list(hour = c(12, 13, 14, 14, 14, 15), date...2 = structure(c(1649721600, 
1649721600, 1649721600, 1649808000, 1646956800, 1646956800), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), time_bike = c(2, 2, 1, 2, 3, 1), distance = c(0, 
0, 0, 0, 0.7463732, 0.24986416), calories = c(0, 0, 0, 0, 11.195598, 
3.7479625), power = c(0.006130556, 0.005802778, 0.002577778, 
0.007138889, 0.008683333, 0.002738889), `Participant Code` = c("AE1_01", 
"AE1_01", "AE1_01", "AE1_01", "AE1_02", "AE1_02"), date...8 = structure(c(1649721600, 
1649721600, 1649721600, 1649808000, 1646956800, 1646956800), tzone = "UTC", class = c("POSIXct", 
"POSIXt"))), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
"data.frame"))

How can I format the date into the this form : yyyy-mm-dd (I don't want the time included)

CodePudding user response:

I believe you can use "yourdata<-as.Date(your_data, "%Y%m/%d/") See this for more info on as.Date

Hope it helps

CodePudding user response:

You can use

as.Date()

so that will make it:

as.Date(ergo_bike$date...2, "%Y-%m-%d")

You can see the syntax on https://www.statmethods.net/input/dates.html

  • Related