Guest User

Untitled

a guest
Jan 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. a <- "Roy lives in Japan and travels to Africa"
  2. b <- "Roy travels Africa with this wife"
  3.  
  4. stra <- as.data.frame(t(read.table(textConnection(a), sep = " ")))
  5. strb <- as.data.frame(t(read.table(textConnection(b), sep = " ")))
  6.  
  7. stra_unique <-as.data.frame(unique(stra$V1))
  8. strb_unique <- as.data.frame(unique(strb$V1))
  9. colnames(stra_unique) <- c("V1")
  10. colnames(strb_unique) <- c("V1")
  11.  
  12. common_words <-length(merge(stra_unique,strb_unique, by = "V1")$V1)
  13.  
  14. > a <- "Roy lives in Japan and travels to Africa"
  15. > b <- "Roy travels Africa with this wife"
  16. > a_split <- unlist(strsplit(a, sep=" "))
  17. > b_split <- unlist(strsplit(b, sep=" "))
  18. > length(intersect(a_split, b_split))
  19. [1] 3
  20.  
  21. vec1 <- c(a,b)
  22. Reduce(`intersect`,str_extract_all(vec1, "\w+"))
  23. #[1] "Roy" "travels" "Africa"
  24.  
  25. library(stringi)
  26. Reduce(`intersect`,stri_extract_all_regex(vec1,"\w+"))
  27. #[1] "Roy" "travels" "Africa"
  28.  
  29. length(Reduce(`intersect`,stri_extract_all_regex(vec1,"\w+")))
  30. #[1] 3
  31.  
  32. Reduce(`intersect`,regmatches(vec1,gregexpr("\w+", vec1)))
  33. #[1] "Roy" "travels" "Africa"
  34.  
  35. a <- "Roy lives in Japan and travels to Africa"
  36. b <- "Roy travels Africa with this wife"
  37. c <- "Bob also travels Africa for trips but lives in the US unlike Roy."
  38.  
  39. library(stringi);library(qdapTools)
  40. X <- stri_extract_all_words(list(a, b, c))
  41. X <- mtabulate(X) > 0
  42. Y <- colSums(X) == nrow(X); names(Y)[Y]
  43.  
  44. [1] "Africa" "Roy" "travels"
Add Comment
Please, Sign In to add comment