我在清理数据方面取得了进展,如下所示:
df1 <- data.frame(ID=(c("18.1010-2.570322","171114-238509","140808-3481906
","18055656193","180625-378224","190903-2793831 / -9311442 / -6810125","190808-625-6692","190 807 - 7941125","1807298087721Roland","19060881t1676")),
True_ID = c("181010-2570322","171114-2385039","190808-4381906","180556-5619343","180625-3782242", "190903-2793831 190903-9311442
190903-6810125", "190808-6256692","190807-7941125","180729-8087721","190608-8112676"))
真实值如下:190312-4184811。所以有一种模式,前六个整数是一个日期,比如19=2019 03=3月,12=一天。其他七个数字是随机的。我清理了很多没有信息的模式,但在这里我不知道如何处理这些不同的模式。
我试过类似的方法,但我认为还有更好的方法:
a = str_extract(data_file$IP_P,"(^|[ ])[:digit:]{6}\-[:digit:]{7}([ ]|$)")
b = str_extract(data_file$IP_P,"(^|[ ])[:digit:]{5}\-[:digit:]{7}([ ]|$)")
c = str_extract(data_file$IP_P,"(^|[ ])[:digit:]{4}\-[:digit:]{7}([ ]|$)")
d = str_extract(data_file$IP_P,"(^|[ ])[:digit:]{6}\-[:digit:]{6}([ ]|$)")
e = str_extract(data_file$IP_P,"(^|[ ])[:digit:]{6}\-[:digit:]{5}([ ]|$)")
f = str_extract(data_file$IP_P,"(^|[ ])[:digit:]{6}\-[:digit:]{4}([ ]|$)")
g = str_extract(data_file$IP_P,"(^|[ ])[:digit:]{6}\-[:digit:]{8}([ ]|$)")
h = str_extract(data_file$IP_P,"(^|[ ])[:digit:]{6}\-[:digit:]{9}([ ]|$)")
data_file["Extracted_i"] = NA
data1 <- data.frame(a,b,c,d,e,f,g,h)
data1 <- data1 %>% unite("z", a:h, remove = FALSE)
data_file["Extracted_i"] =gsub("[^0-9\.\-]", "", data1$z)
难道你不能去掉所有非数字字符,给出一个由所有数字组成的字符串,然后用一个连接"将第一个6和第二个6粘贴在一起吗-"?
paste(substr(gsub("\D", "", df1$ID), 1, 6),
substr(gsub("\D", "", df1$ID), 7, 12),
sep = "-")
#> [1] "181010-257032" "171114-238509" "140808-348190" "180556-56193"
#> [5] "180625-378224" "190903-279383" "190808-625669" "190807-794112"
#> [9] "180729-808772" "190608-811676"
我们也可以使用gsub
将字符捕获为一个组,并在替换中指定捕获组的后引用(\1
,\2
(
gsub("^(.{1,6})(.{1,6}).*", "\1-\2", gsub("\D+", "", df1$ID))
#[1] "181010-257032" "171114-238509" "140808-348190" "180556-56193"
#[5] "180625-378224" "190903-279383" "190808-625669" "190807-794112"
#[9] "180729-808772" "190608-811676"