【发布时间】:2020-09-12 19:22:31
【问题描述】:
我有一个数据框,其中包含 YYYYMMDD 格式的日期列。我想将此列值转换为 YYYY_MM_DD 以匹配另一个数据库中的格式。
我发现的解决方案主要集中在拆分字符串,例如逗号分隔。
我基本上想要一个索引 YYYYMMDD 并在第 4 个字符和第 6 个字符之后插入“_”的解决方案。
谢谢
更新:
发布后,我几乎立即找到了适合我的解决方案:
library(tidyr)
# create names for new columns
newCol <- c("YEAR", "MONTH", "DAY")
# separate existing column at 4 and 6 character
newData <- separate(table, old_column, newCol, sep = c(4,6))
# combining the three columns to one delimited by '_'
table$newColumn <- paste(table$YEAR, table$MONTH, table$DAY, sep = "_")
【问题讨论】:
标签: r date indexing split character