fuzzyjoin icon indicating copy to clipboard operation
fuzzyjoin copied to clipboard

Add vignette on fuzzy_left_join()

Open Dallak opened this issue 3 years ago • 0 comments

Hi David,

Thank you for all you have done for the R community.

This is just to provide you with an example case of using fuzzy_left_join() in order to add it to the vignette in response to your call.

I posted the following question at RStudio Community, and a member answered my question elegantly using fuzzy_left_join().

Here is the post.

https://community.rstudio.com/t/adding-a-new-column-based-on-different-ranges-of-number-stored-in-vectors/128950?u=dallak

For ease of reading, I posted the following question:

I have this dataset, I want to add a new column for different ranges of number based on day. The ranges of number I am interested on are stored in v1 and v2.


ex <- data.frame('id'= seq(1:26),
                 'day'= c(105:115, 1:12,28:30),
                 'letter' = LETTERS[1:26],
                 s = rep(1:26, each = 3, len = 26) )

v1 <- c(107,112,10)
v2 <- c(109,115,28)

word <- c("pen","desk","light")

The column I want to add is stored in word. From day 107:109 I want to add the word "pen", 112:115 "desk", and 10:28 "light". For the rest of the values that fall outside of these ranges, I want to use the word "undetermined".

And a helpful member provided this answer.

ex <- data.frame('id'= seq(1:26),
                  'day'= c(105:115, 1:12,28:30),
                  'letter' = LETTERS[1:26],
                  s = rep(1:26, each = 3, len = 26) )
v1 <- c(107,112,10)
v2 <- c(109,115,28)
word <- c("pen","desk","light")
Ranges <- data.frame(v1,v2,word)
library(dplyr)
library(fuzzyjoin)
library(tidyr)
ex2 <- ex |> fuzzy_left_join(Ranges, by = c(day = "v1", day = "v2"),
                              match_fun = list(`>=`, `<=`)) |> 
   mutate(word = replace_na(word, "undetermined")) |> 
   select(-v1, -v2)
ex2

I hope this helps!

Dallak avatar Feb 12 '22 05:02 Dallak