我有一个列在我的数据框架,这是一些日期和字符串值的混合。我想明确地选择日期并转换为UNIX时间戳,并保留字符串值。如何做到这一点呢?
示例数据|column1|
---------
|2020-12-21 00:00:00|
|test1|
|test2|
|test3|
|2021-12-21 00:00:00|
预期的结果
|Column1|
---------------
|1608508800|
|test1|
|test2|
|test3|
|1608508800|
x = read.table(text = 'column1
2020-12-21 00:00:00
test1
test2
test3
2021-12-21 00:00:00', sep = ";", header = T)
uts = as.numeric(as.POSIXct(x$column1, format = "%Y-%m-%d %H:%M:%S", tz = "UTC"))
uts_i = which(!is.na(uts))
x$column1[uts_i] = uts[uts_i]
x
# column1
# 1 1608508800
# 2 test1
# 3 test2
# 4 test3
# 5 1640044800
或与dplyr
:
x %>%
mutate(
uts = as.numeric(as.POSIXct(x$column1, format = "%Y-%m-%d %H:%M:%S", tz = "UTC")),
column1 = coalesce(as.character(uts), column1)
) %>%
select(-uts)
# column1
# 1 1608508800
# 2 test1
# 3 test2
# 4 test3
# 5 1640044800