R - How can I add an empty POSIXct column to a data.frame / tibble which already exists?

久未见 提交于 2019-12-02 00:55:21

would this work for you (most doing what eipi10 suggest in his comment)

library(tibble) # install.packages(c("dplyr"), dependencies = TRUE)
df <- tibble(a = 1:3, b = letters[a], c = as.POSIXct(NA))

df 
#> # A tibble: 3 x 3
#>       a     b      c
#>   <int> <chr> <dttm>
#> 1     1     a     NA
#> 2     2     b     NA
#> 3     3     c     NA

str(df)
#> Classes ‘tbl_df’, ‘tbl’ and 'data.frame':
#>    3 obs. of  3 variables:
#> $ a: int  1 2 3
#> $ b: chr  "a" "b" "c"
#> $ c: POSIXct, format: NA NA ...

or maybe

df <- tibble(a = numeric(), b = character(), c = as.POSIXct(NA))
str(df)
#> Classes ‘tbl_df’, ‘tbl’ and 'data.frame':
#>   0 obs. of  3 variables:
#> $ a: num 
#> $ b: chr 
#> $ c:Classes 'POSIXct', 'POSIXt'  num(0) 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!