Question: Date functions keeps returning null values

Hello,
I am new to R. I started working with R about a week ago.
I have been trying to format the date and time and instead of changing to datetime, I get nulls instead

daily_steps <- daily_steps %>%
  rename(Date = ActivityDay) %>%
  mutate(Date = as.Date(date,format ="%m/%d/%Y %I:%M:%S %p"))

I also tried this

daily_steps$ActivityDay <- as.Date(daily_steps$ActivityDay, format="%m-%d-%y")

but I also got null values.

Please Help
Thank you

What do the character "dates look like?

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here between
```

```

Both of these work for me.

xx  <- "12-07-2023"
 as.Date(xx, format="%m-%d-%y")
 
yy  <- "12/07/2023 11:31:15"
as.Date(yy, "%m/%d/%Y  %H:%M:%S")
dput(head(daily_calories,100))
structure(list(Id = c(1503960366, 1503960366, 1503960366, 1503960366, 
1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 
1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 
1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 
1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 
1503960366, 1503960366, 1503960366, 1624580081, 1624580081, 1624580081, 
1624580081, 1624580081, 1624580081, 1624580081, 1624580081, 1624580081, 
1624580081, 1624580081, 1624580081, 1624580081, 1624580081, 1624580081, 
1624580081, 1624580081, 1624580081, 1624580081, 1624580081, 1624580081, 
1624580081, 1624580081, 1624580081, 1624580081, 1624580081, 1624580081, 
1624580081, 1624580081, 1624580081, 1624580081, 1644430081, 1644430081, 
1644430081, 1644430081, 1644430081, 1644430081, 1644430081, 1644430081, 
1644430081, 1644430081, 1644430081, 1644430081, 1644430081, 1644430081, 
1644430081, 1644430081, 1644430081, 1644430081, 1644430081, 1644430081, 
1644430081, 1644430081, 1644430081, 1644430081, 1644430081, 1644430081, 
1644430081, 1644430081, 1644430081, 1644430081, 1844505072, 1844505072, 
1844505072, 1844505072, 1844505072, 1844505072, 1844505072, 1844505072
), ActivityDay = c("4/12/2016", "4/13/2016", "4/14/2016", "4/15/2016", 
"4/16/2016", "4/17/2016", "4/18/2016", "4/19/2016", "4/20/2016", 
"4/21/2016", "4/22/2016", "4/23/2016", "4/24/2016", "4/25/2016", 
"4/26/2016", "4/27/2016", "4/28/2016", "4/29/2016", "4/30/2016", 
"5/1/2016", "5/2/2016", "5/3/2016", "5/4/2016", "5/5/2016", "5/6/2016", 
"5/7/2016", "5/8/2016", "5/9/2016", "5/10/2016", "5/11/2016", 
"5/12/2016", "4/12/2016", "4/13/2016", "4/14/2016", "4/15/2016", 
"4/16/2016", "4/17/2016", "4/18/2016", "4/19/2016", "4/20/2016", 
"4/21/2016", "4/22/2016", "4/23/2016", "4/24/2016", "4/25/2016", 
"4/26/2016", "4/27/2016", "4/28/2016", "4/29/2016", "4/30/2016", 
"5/1/2016", "5/2/2016", "5/3/2016", "5/4/2016", "5/5/2016", "5/6/2016", 
"5/7/2016", "5/8/2016", "5/9/2016", "5/10/2016", "5/11/2016", 
"5/12/2016", "4/12/2016", "4/13/2016", "4/14/2016", "4/15/2016", 
"4/16/2016", "4/17/2016", "4/18/2016", "4/19/2016", "4/20/2016", 
"4/21/2016", "4/22/2016", "4/23/2016", "4/24/2016", "4/25/2016", 
"4/26/2016", "4/27/2016", "4/28/2016", "4/29/2016", "4/30/2016", 
"5/1/2016", "5/2/2016", "5/3/2016", "5/4/2016", "5/5/2016", "5/6/2016", 
"5/7/2016", "5/8/2016", "5/9/2016", "5/10/2016", "5/11/2016", 
"4/12/2016", "4/13/2016", "4/14/2016", "4/15/2016", "4/16/2016", 
"4/17/2016", "4/18/2016", "4/19/2016"), Calories = c(1985, 1797, 
1776, 1745, 1863, 1728, 1921, 2035, 1786, 1775, 1827, 1949, 1788, 
2013, 1970, 2159, 1898, 1837, 1947, 1820, 2004, 1990, 1819, 1959, 
1896, 1821, 1740, 1819, 1859, 1783, 0, 1432, 1411, 1572, 1344, 
1463, 1554, 1604, 1435, 1446, 1467, 1470, 1562, 1617, 1492, 1402, 
1670, 1401, 1404, 1655, 2690, 1497, 1334, 1368, 1370, 1341, 1474, 
1427, 1328, 1393, 1359, 1002, 3199, 2902, 3226, 2750, 3493, 3011, 
2806, 3300, 2430, 2140, 2344, 2677, 2413, 2497, 3123, 2489, 3108, 
2498, 3846, 2696, 2580, 3324, 2222, 2463, 3328, 3404, 2987, 3008, 
2799, 1276, 2030, 1860, 2130, 1725, 1657, 1793, 1814, 1366)), row.names = c(NA, 
-100L), class = c("tbl_df", "tbl", "data.frame"))

This is the output

It does seem to work like that:

daily_calories <- ...

daily_calories %>%
  mutate(Date = as.Date(ActivityDay, format ="%m/%d/%Y"))
#> # A tibble: 100 × 4
#>            Id ActivityDay Calories Date      
#>         <dbl> <chr>          <dbl> <date>    
#>  1 1503960366 4/12/2016       1985 2016-04-12
#>  2 1503960366 4/13/2016       1797 2016-04-13
#>  3 1503960366 4/14/2016       1776 2016-04-14
#>  4 1503960366 4/15/2016       1745 2016-04-15
#>  5 1503960366 4/16/2016       1863 2016-04-16
#>  6 1503960366 4/17/2016       1728 2016-04-17
#>  7 1503960366 4/18/2016       1921 2016-04-18
#>  8 1503960366 4/19/2016       2035 2016-04-19
#>  9 1503960366 4/20/2016       1786 2016-04-20
#> 10 1503960366 4/21/2016       1775 2016-04-21
#> # ℹ 90 more rows

Created on 2023-12-11 with reprex v2.0.2

1 Like

This seems fine too, where I named your file dat2. Check that your format = statement is correct.

dat2$ActivityDay  <- as.Date(dat2$ActivityDay, format="%m/%d/%Y")
1 Like

Thank you!
It worked :smile:

Thank you so much! :smile:

Attempting to wrangle with datetime formatting issues, my humble Dash app seems to dance with the void, replacing date and time with the eerie presence of nulls. In a quest to harmonize the temporal dimensions, I've attempted the arcane arts of renaming and mutating columns, invoking the mighty as.POSIXct spell, yet the elusive datetime format remains elusive.

Hello Erik, if you would like support, consider starting a thread on the forum.

thank you ite really helpful :slightly_smiling_face: