gpt4 book ai didi

regex - 从字符串和文本数据中提取年份

转载 作者:行者123 更新时间:2023-12-05 00:56:48 25 4
gpt4 key购买 nike

我需要从具有这些性质的值的向量中提取开始年份和结束年份。

 yr<- c("June 2013 – Present (2 years 9 months)", "January 2012 – June 2013 (1 year 6 months)","2006 – Present (10 years)","2002 – 2006 (4 years)")


yr
June 2013 – Present (2 years 9 months)
January 2012 – June 2013 (1 year 6 months)
2006 – Present (10 years)
2002 – 2006 (4 years)

我期待这样的输出。有没有人有建议?
 start_yr       end_yr

2013 2016
2012 2013
2006 2016
2002 2006

最佳答案

x <- gsub("present", "2016", yr, ignore.case = TRUE)
x <- regmatches(x, gregexpr("\\d{4}", x))
start_yr <- sapply(x, "[[", 1)
end_yr <- sapply(x, "[[", 2)

这将开始年份和结束年份保存在 2 个单独的变量中,如果您希望将它们放在一个变量中,只需编辑代码并使 y$start_yr y$end_yr

关于regex - 从字符串和文本数据中提取年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35710387/

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