将列中的特定值转换为 UNIX 时间戳
Convert Specific values in a column to UNIX timestamp
我的数据框中有一列混合了一些日期和字符串值。我想专门选择日期并转换为 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
我的数据框中有一列混合了一些日期和字符串值。我想专门选择日期并转换为 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