Hello,

The first column of data has the mixed format of character and Posixct.
In addition, this column is in list format.
Could anyone suggest how can I convert the character to date format?

dg <- drive_get("https://docs.google.com/spreadsheets/d/1_OOqInR7-ITwxpSi_gka82zGeF5naeFIwuN7BOmMVos/edit?usp=sharing")
dg_dat <- sheets_read(dg$id,col_names = TRUE)
str(dg_dat$Date)

I'm not sure what data you're using but I created an example that might be structured as you described - it uses the tidyverse.

library(tidyverse)
 df <- tibble(Date = list(as.Date("2001-01-05"), 
                          as.Date("2015-09-05"), 
                          as.Date("2075-12-05")), 
                          var = c(1, 3, 4))

The df looks like this printed out:

> df
# A tibble: 3 x 2
  Date         var
  <list>     <dbl>
1 <date [1]>     1
2 <date [1]>     3
3 <date [1]>     4

You could expand the list of dates like so:

df %>% unnest(cols = c(Date))

  Date         var
  <date>     <dbl>
1 2001-01-05     1
2 2015-09-05     3
3 2075-12-05     4

In addition, could you check with at least one of the row should be character as

into

 df <- tibble(Date = list(as.character("2001-01-05"), 
                         as.Date("2015-09-05"), 
                         as.Date("2075-12-05")), 
             var = c(1, 3, 4))

Try this:

df %>% 
  mutate(Datex = map_chr(Date, as.character))

Which you could bake inside an as.Date like:

df %>% 
  mutate(Datex = as.Date(map_chr(Date, as.character)))

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.