I have test data as follows. I am trying to find (near) matches for a vector of words, using stringdist
as the actual database is large:
library(stringdist)
test_data <- structure(list(Province = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3), Year = c(2000,
2000, 2000, 2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000,
2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000, 2001, 2001,
2001, 2002, 2002, 2002), Municipality = c("Some", "Anything",
"Nothing", "Someth.", "Anything", "Not", "Something", "Anything",
"None", "Some", "Anything", "Nothing", "Someth.", "Anything",
"Not", "Something", "Anything", "None", "Some", "Anything", "Nothing",
"Someth.", "Anything", "Not", "Something", "Anything", "None"
), `Other Values` = c(0.41, 0.42, 0.34, 0.47, 0.0600000000000001,
0.8, 0.14, 0.15, 0.01, 0.41, 0.42, 0.34, 0.47, 0.0600000000000001,
0.8, 0.14, 0.15, 0.01, 0.41, 0.42, 0.34, 0.47, 0.0600000000000001,
0.8, 0.14, 0.15, 0.01)), row.names = c(NA, -27L), class = c("tbl_df",
"tbl", "data.frame"))
# A tibble: 27 x 4
Province Year Municipality `Other Values`
<dbl> <dbl> <chr> <dbl>
1 1 2000 Some 0.41
2 1 2000 Anything 0.42
3 1 2000 Nothing 0.34
4 1 2001 Someth. 0.47
5 1 2001 Anything 0.0600
6 1 2001 Not 0.8
7 1 2002 Something 0.14
8 1 2002 Anything 0.15
9 1 2002 None 0.01
10 2 2000 Some 0.41
# ... with 17 more rows
I tried to run:
test_match_out <- amatch(c("Anything","Something"),test_data[,3],maxDist=2)
EDIT:
Following the comment of zx8754 , I tried:
test_match_out <- amatch(c("Anything","Something"),test_data[[3]],maxDist=2)
And:
test_match_out <- amatch(c("Anything","Something"),test_data$Municipality,maxDist=2)
I was under the impression that the previous line (amatch
) would give me something like a vector of indices, where there would be a match. But it just gives me a vector with two NA
values. Am I misunderstanding what amatch
does, or is there something wrong in the syntax?
I want to get the values
for which amatch
is a match and the word that is matched.
Desired output:
test_data_2 <- structure(list(Province = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3), Year = c(2000,
2000, 2000, 2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000,
2001, 2001, 2001, 2002, 2002, 2002, 2000, 2000, 2000, 2001, 2001,
2001, 2002, 2002, 2002), Municipality = c("Some", "Anything",
"Nothing", "Someth.", "Anything", "Not", "Something", "Anything",
"None", "Some", "Anything", "Nothing", "Someth.", "Anything",
"Not", "Something", "Anything", "None", "Some", "Anything", "Nothing",
"Someth.", "Anything", "Not", "Something", "Anything", "None"
), `Other Values` = c(0.41, 0.42, 0.34, 0.47, 0.0600000000000001,
0.8, 0.14, 0.15, 0.01, 0.41, 0.42, 0.34, 0.47, 0.0600000000000001,
0.8, 0.14, 0.15, 0.01, 0.41, 0.42, 0.34, 0.47, 0.0600000000000001,
0.8, 0.14, 0.15, 0.01), `Matched Values` = c(NA, 0.42, NA, NA, 0.06000,
NA, 0.14, 0.15, NA, NA, 0.42, NA, NA, 0.0600000000000001,
NA, 0.14, 0.15, NA, NA, 0.42, NA, NA, 0.0600000000000001,
NA, 0.14, 0.15, NA), `Matched Values` = c(NA, "Anything", NA, NA, "Anything",
NA, "Something", "Anything", NA, NA, "Anything", NA, NA, "Anything",
NA, "Something", "Anything", NA, NA, "Anything", NA, NA, "Anything",
NA, "Something", "Anything", NA)), row.names = c(NA, -27L), class = c("tbl_df",
"tbl", "data.frame"))
CodePudding user response:
Get the index of matches, then update all rows that match:
ix <- amatch(c("Anything","Something"), test_data[[ 3 ]], maxDist = 2)
# [1] 2 7
ifelse(test_data$Municipality %in% test_data$Municipality[ ix ],
test_data$`Other Values`, NA)
# [1] NA 0.42 NA NA 0.06 NA 0.14 0.15 NA NA 0.42
# [12] NA NA 0.06 NA 0.14 0.15 NA NA 0.42 NA NA
# [23] 0.06 NA 0.14 0.15 NA