Fatima
Fatima

Reputation: 495

How to fix the return of the matched elements in r

I have two data frame and I want to match them together and return the element back but the problem I got is if there is a match it returns the first element, not the matching element.

I tried this code but it returns the index of the element

data3 <- data1 %>%
   mutate(score = lapply(M,function(x){ifelse (f<-(match(x, data2$words)), f )}))

  Term score
1    I    NA
2  A B  3, 7
3    A     3
4    Z    NA
5    D     4
6    B     7 

here is the code :

data1 <- data.frame(Term = c("I","A B","A","Z","D","B"))#txt
library(stringr)
M<-str_split(data1$Term , pattern = "\\s+")
data2 <- data.frame(words = c("O","C","A","D","E","F","B"))#dec
data3 <- data1 %>% mutate(score = lapply(M,function(x){ifelse (match(x, data2$words), data2$words )}))```

#here is the result I got 

 Term score
1    I    NA
2  A B  O, C
3    A     O
4    Z    NA
5    D     O
6    B     O

#and as you can see if there is a match it returns the first element.

#the result I expected 

  Term score
1    I    NA
2  A B  A, B
3    A     A
4    Z    NA
5    D     D
6    B     B

Upvotes: 1

Views: 117

Answers (1)

akrun
akrun

Reputation: 887028

After getting the index, use that to get the corresponding 'words' from 'data2', paste the elements into a single string (toString) on the non-NA elements and change the blank ("") elements to NA with dplyr::na_if

data1$score <- sapply(M, function(x) {
       x1 <- data2$words[match(x, data2$words)]
       dplyr::na_if(toString(na.omit(x1)), "")})
data1$score
#[1] NA     "A, B" "A"    NA     "D"    "B"   

Upvotes: 1

Related Questions