我期望從列中的條目(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)? –