I use readr
to read in data which consists a date column in time format. I can read it in correctly using the col_types
option of readr
.
library(dplyr)
library(readr)
sample <- "time,id
2015-03-05 02:28:11,1674
2015-03-03 13:10:59,36749
2015-03-05 07:55:48,NA
2015-03-05 06:13:19,NA
"
mydf <- read_csv(sample, col_types="Ti")
mydf
time id
1 2015-03-05 02:28:11 1674
2 2015-03-03 13:10:59 36749
3 2015-03-05 07:55:48 NA
4 2015-03-05 06:13:19 NA
This is nice. However, if I want to manipulate this column with dplyr
, the time column loses its format.
mydf %>% mutate(time = ifelse(is.na(id), NA, time))
time id
1 1425522491 1674
2 1425388259 36749
3 NA NA
4 NA NA
Why is this happening?
I know I can work around this problem by transforming it to character before, but it would be more convenient without transforming back and forth.
mydf %>% mutate(time = as.character(time)) %>%
mutate(time = ifelse(is.na(id), NA, time))