Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vignette on fuzzy_left_join() #83

Open
Dallak opened this issue Feb 12, 2022 · 0 comments
Open

Add vignette on fuzzy_left_join() #83

Dallak opened this issue Feb 12, 2022 · 0 comments

Comments

@Dallak
Copy link

Dallak commented Feb 12, 2022

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant