gagan
January 23, 2022, 2:15pm
1
Hi, i want to convert date time format from characters to date format by using lubridate package. The date time to be converted is 5/2/2016 11:59:56 PM
col2_dates <-parse_date_time(date[, 2], orders = "%m %d %Y %H:%M:%S")
On running the above code, the error i get is as below, can someone please help. thanks
Error in [.default
(date, , 2) : incorrect number of dimensions
Mydate <- "5/2/2016 11:59:56 PM"
as_datetime(Mydate, format = "%d/%m/%Y %I:%M:%S %p")
as_date(Mydate, format = "%d/%m/%Y %I:%M:%S %p")
or change day with month.
gagan
January 23, 2022, 2:30pm
3
Thanks. The date is in column 2 as indicated below, how to change in date format for all values in column 2
A tibble: 6 x 8
id date weightkg weightpounds fat bmi ismanualreport logid
<dbl> <chr> <dbl> <dbl> <dbl> <dbl> <lgl> <dbl>
1 1503960366 5/2/2016 11:59:59 PM 52.6 116. 22 22.6 TRUE 1462233599000
2 1503960366 5/3/2016 11:59:59 PM 52.6 116. NA 22.6 TRUE 1462319999000
3 1927972279 4/13/2016 1:08:52 AM 134. 294. NA 47.5 FALSE 1460509732000
4 2873212765 4/21/2016 11:59:59 PM 56.7 125. NA 21.5 TRUE 1461283199000
5 2873212765 5/12/2016 11:59:59 PM 57.3 126. NA 21.7 TRUE 1463097599000
6 4319703577 4/17/2016 11:59:59 PM 72.4 160. 25 27.5 TRUE 1460937599000
library(dplyr)
library(lubridate)
#preparing the data set
List <- "
1503960366 5/2/2016 11:59:59 PM 52.6 116. 22 22.6 TRUE 1462233599000
1503960366 5/3/2016 11:59:59 PM 52.6 116. NA 22.6 TRUE 1462319999000
1927972279 4/13/2016 1:08:52 AM 134. 294. NA 47.5 FALSE 1460509732000
2873212765 4/21/2016 11:59:59 PM 56.7 125. NA 21.5 TRUE 1461283199000
2873212765 5/12/2016 11:59:59 PM 57.3 126. NA 21.7 TRUE 1463097599000
4319703577 4/17/2016 11:59:59 PM 72.4 160. 25 27.5 TRUE 1460937599000
"
df <-
read.csv(textConnection(List), skip = 1, sep = " ", as.is = TRUE, header = FALSE,
col.names = c( "id","date","time","ampm","weightkg","weightpounds","fat","bmi","ismanualreport","logi")
)
df <-
df |> mutate(newdate = paste(date, time, ampm, sep = " "), .before = date) |>
select(-c(date, time, ampm)) |>
rename(date = newdate)
head(df)
#> id date weightkg weightpounds fat bmi
#> 1 1503960366 5/2/2016 11:59:59 PM 52.6 116 22 22.6
#> 2 1503960366 5/3/2016 11:59:59 PM 52.6 116 NA 22.6
#> 3 1927972279 4/13/2016 1:08:52 AM 134.0 294 NA 47.5
#> 4 2873212765 4/21/2016 11:59:59 PM 56.7 125 NA 21.5
#> 5 2873212765 5/12/2016 11:59:59 PM 57.3 126 NA 21.7
#> 6 4319703577 4/17/2016 11:59:59 PM 72.4 160 25 27.5
#> ismanualreport logi
#> 1 TRUE 1.462234e+12
#> 2 TRUE 1.462320e+12
#> 3 FALSE 1.460510e+12
#> 4 TRUE 1.461283e+12
#> 5 TRUE 1.463098e+12
#> 6 TRUE 1.460938e+12
Right now I have similar data set to yours, I assume.
Lets create another column with proper date format:
df |> mutate(MyNewDate = as_date(date, format = "%m/%d/%Y %I:%M:%S %p"))
#> id date weightkg weightpounds fat bmi
#> 1 1503960366 5/2/2016 11:59:59 PM 52.6 116 22 22.6
#> 2 1503960366 5/3/2016 11:59:59 PM 52.6 116 NA 22.6
#> 3 1927972279 4/13/2016 1:08:52 AM 134.0 294 NA 47.5
#> 4 2873212765 4/21/2016 11:59:59 PM 56.7 125 NA 21.5
#> 5 2873212765 5/12/2016 11:59:59 PM 57.3 126 NA 21.7
#> 6 4319703577 4/17/2016 11:59:59 PM 72.4 160 25 27.5
#> ismanualreport logi MyNewDate
#> 1 TRUE 1.462234e+12 2016-05-02
#> 2 TRUE 1.462320e+12 2016-05-03
#> 3 FALSE 1.460510e+12 2016-04-13
#> 4 TRUE 1.461283e+12 2016-04-21
#> 5 TRUE 1.463098e+12 2016-05-12
#> 6 TRUE 1.460938e+12 2016-04-17
Grzegorz
system
Closed
February 13, 2022, 4:49pm
5
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.