小编Oli*_*ver的帖子

在 R 中查找随机数据中的年份

我的数据框中有 71 列,其中 10 列包含的数据可能包含 1990 年至 2019 年之间的年份,格式为 YYYY(例如 2019)。例如:

id_1 <- c("regkfg_2013", "fsgdf-2014", "f2016sghsg", "gjdg1990_3759")
id_2 <- c("dghdgl2013jg", "2fgdg_2014_hf", "ghdg_2016*89", "gc-hs1990")
Run Code Online (Sandbox Code Playgroud)

我正在尝试找到一种方法从相关单元格中提取年份并将其插入新列中。

到目前为止,我只知道如何以非常耗时的方式过滤数据。我生成了以下代码,其开头如下:

dated_data <- select(undated_data, 1:71) %>% 
                filter(grepl("1990", id_1) | filter(grepl("1990", id_2) | filter(grepl("1991", id_1) | filter(grepl("1991", id_2)
Run Code Online (Sandbox Code Playgroud)

然而,写完这十个专栏、整整三十年,真的需要很长的时间。我确信有更快的方法。我也不知道如何将每个匹配单元格中的日期提取到新单元格中。

我想要的输出如下所示:

dated_data$year <- c("2013", "2014", "2016", "1990")

有谁知道我该怎么做?预先感谢您的帮助!

regex r

1
推荐指数
1
解决办法
887
查看次数

标签 统计

r ×1

regex ×1