我期望从列中的条目(user_entry
)具有不同格式并且每行可能包含多个实例的数据创建查找表。从定义不明的用户输入数据中提取多个字符串
# create example dataframe.
id <- c(1111,1112,1113,1114)
user_entry <- c("999/1001","1002;1003","999/1004\n999/1005","9991006 9991007")
df <- data.frame(id,user_entry)
> df
id user_entry
1 1111 999/1001
2 1112 1002;1003
3 1113 999/1004\n999/1005
4 1114 9991006 9991007
我只在其可以或可以不被一个3位的位置的代码和/或分隔符之前诸如“/”或空间中的4位代码感兴趣。每个条目中可能有多个4位数的代码,我想在最终的查找表中分别列出每个代码(请参见下面的lookup
)。
下面的代码做我正在寻找,但真正不合适循环内循环和内部增长的数据框。有没有更好的方法来做到这一点?
library(dplyr);library(stringr)
# use stringr package to extract only digits
df <- df %>%
mutate(entries = str_extract_all(user_entry,"[[:digit:]]+")) %>%
select(-user_entry)
# initialise lookup dataframe
lookup <- df[FALSE,]
for (record in 1:nrow(df)){
entries <- df$entries[[record]]
for (element in 1:length(entries)){
# only interested in 4 digit codes
if (nchar(entries[element])>3){
# remove 3 digit code if it is still attached
lookup_entry <- gsub('.*?(\\d{4})$','\\1',entries[element])
lookup <- rbind(lookup,data.frame(id=df$id[[record]],entries=lookup_entry))
}
}
}
> lookup
id entries
1 1111 1001
2 1112 1002
3 1112 1003
4 1113 1004
5 1113 1005
6 1114 1006
7 1114 1007
也许你可以提取每一个数字的最后4位数字序列? ['str_extract_all(user_entry, “\\ d {4} \\ B”)'](https://regex101.com/r/Hm20nm/1)? –