partial matching of strings in different two datasets to obtain a match with higher frequency

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago


I have strings in two datasets and i would like to do a partial match. Here is the code that I have written

 df1 <- data.frame(A=c(.87,.11,.44,.45), B=c("I have a beard", "I slept for two hours", "I have had two courses","this is not true"))

 df2 <- data.frame(X=c(127,10,433,344,890,4),Y=c("have","beard","syllabus","true","three","maths"))

I want to do a pmatch and I am expecting output as follows

  A     B                            X      Y
.87   I have a beard               127      have
.11   I slept for two hours        NA       NA
.44   I have had two courses       127      have
.45   this is not true             344      true

I would like to a partial match with a left join on df1. I want to get the higher of the two matches(for example in "I have a beard" string "have" match has 127 and "beard" has 10 and i want to get the higher match. Any suggestions?

profilepic.png
manpreet 2 years ago

This dplyr method doesn't need a join (which is reasonable as you don't have a common column to join on). It combines the 2 datasets and finds the matches. As long as you don't have thousands of rows it will work fast enough. Of course you can make the script smaller, but you can run this step by step to see how it works.

df1<- data.frame(A=c(.87,.11,.44,.45), B=c("I have a beard", "I slept for two hours", "I have had two courses","this is not true"))

df2<- data.frame(X=c(127,10,433,344,890,4),Y=c("have","beard","syllabus","true","three","maths"))

library(dplyr)

df1 %>% 
  rowwise() %>%
  do(data.frame(.,df2)) %>%                    # combine datasets
  do(data.frame(.,flag = grepl(.$Y,.$B))) %>%  # for each row check if there's a match and name it flag
  ungroup %>%
  group_by(A,B) %>%                            # for each A and B
  mutate(N=sum(flag)) %>%                      # count how many matches you have
  filter(flag==TRUE | N == 0) %>%              # keep only A,B where you have some matches or no match at all
  top_n(1,X) %>%                               # pick one row based on max value of X
  ungroup %>%
  mutate(Y = ifelse(flag==FALSE,NA,as.character(Y)),   # if there's no match replace Y with NA
         X = ifelse(flag==FALSE,NA,X)) %>%             # if there's no match replace X with NA
  select(-c(flag,N)) 


#      A                      B   X    Y
# 1 0.87         I have a beard 127 have
# 2 0.11  I slept for two hours  NA   NA
# 3 0.44 I have had two courses 127 have
# 4 0.45       this is not true 344 true

Try to experiment and change various column values to see how it works. You might be able to spot any bugs in advance.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.