gpt4 book ai didi

r - 将字符串列拆分为年月日

转载 作者:行者123 更新时间:2023-12-02 01:27:43 26 4
gpt4 key购买 nike

我有一个数据集,其中的 info 列与下面的数据类似。如何将其拆分为年、月、日列?

代码:

  df = structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8), info = c("PRISM_ppt_provisional_4kmD2_20220925_bil", 
"PRISM_ppt_provisional_4kmD2_20220926_bil", "PRISM_ppt_provisional_4kmD2_20220927_bil",
"PRISM_ppt_provisional_4kmD2_20220928_bil", "PRISM_ppt_provisional_4kmD2_20220929_bil",
"PRISM_ppt_provisional_4kmD2_20220930_bil", "PRISM_ppt_provisional_4kmD2_20220925_bil",
"PRISM_ppt_provisional_4kmD2_20220926_bil")), class = "data.frame", row.names = c(NA,
-8L))

desired_df = structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8), info = c("PRISM_ppt_provisional_4kmD2_20220925_bil",
"PRISM_ppt_provisional_4kmD2_20220926_bil", "PRISM_ppt_provisional_4kmD2_20220927_bil",
"PRISM_ppt_provisional_4kmD2_20220928_bil", "PRISM_ppt_provisional_4kmD2_20220929_bil",
"PRISM_ppt_provisional_4kmD2_20220930_bil", "PRISM_ppt_provisional_4kmD2_20220925_bil",
"PRISM_ppt_provisional_4kmD2_20220926_bil"), year = c(2022, 2022,
2022, 2022, 2022, 2022, 2022, 2022), month = c(9, 9, 9, 9, 9,
9, 9, 9), day = c(25, 26, 27, 28, 29, 30, 25, 26)), class = "data.frame", row.names = c(NA,
-8L))

# Extract year, month and day from info column
df = separate(df, info, into = c("year", "month", "day"), sep = ?, convert = T)

最佳答案

在这种情况下最好使用extract:

library(tidyr)
df %>%
extract(info, "PRISM_ppt_provisional_4kmD2_(\\d{4})(\\d{2})(\\d{2})_bil",
into = c("year", "month", "day"), remove = F)

# id info year month day
# 1 1 PRISM_ppt_provisional_4kmD2_20220925_bil 2022 09 25
# 2 2 PRISM_ppt_provisional_4kmD2_20220926_bil 2022 09 26
# 3 3 PRISM_ppt_provisional_4kmD2_20220927_bil 2022 09 27
# 4 4 PRISM_ppt_provisional_4kmD2_20220928_bil 2022 09 28
# 5 5 PRISM_ppt_provisional_4kmD2_20220929_bil 2022 09 29
# 6 6 PRISM_ppt_provisional_4kmD2_20220930_bil 2022 09 30
# 7 7 PRISM_ppt_provisional_4kmD2_20220925_bil 2022 09 25
# 8 8 PRISM_ppt_provisional_4kmD2_20220926_bil 2022 09 26

如果您的最终目标是创建一个日期列,那么这可能会更好:

library(tidyr)
library(lubridate)
df %>%
extract(info, "PRISM_ppt_provisional_4kmD2_(.*)_bil",
into = "date", remove = F) %>%
mutate(date = ymd(date),
year = year(date),
month = month(date),
day = day(date))

关于r - 将字符串列拆分为年月日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74056262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com