열 (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
는 I 또는 "/"또는 공간으로서 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)? –