Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a <- "Roy lives in Japan and travels to Africa"
- b <- "Roy travels Africa with this wife"
- stra <- as.data.frame(t(read.table(textConnection(a), sep = " ")))
- strb <- as.data.frame(t(read.table(textConnection(b), sep = " ")))
- stra_unique <-as.data.frame(unique(stra$V1))
- strb_unique <- as.data.frame(unique(strb$V1))
- colnames(stra_unique) <- c("V1")
- colnames(strb_unique) <- c("V1")
- common_words <-length(merge(stra_unique,strb_unique, by = "V1")$V1)
- > a <- "Roy lives in Japan and travels to Africa"
- > b <- "Roy travels Africa with this wife"
- > a_split <- unlist(strsplit(a, sep=" "))
- > b_split <- unlist(strsplit(b, sep=" "))
- > length(intersect(a_split, b_split))
- [1] 3
- vec1 <- c(a,b)
- Reduce(`intersect`,str_extract_all(vec1, "\w+"))
- #[1] "Roy" "travels" "Africa"
- library(stringi)
- Reduce(`intersect`,stri_extract_all_regex(vec1,"\w+"))
- #[1] "Roy" "travels" "Africa"
- length(Reduce(`intersect`,stri_extract_all_regex(vec1,"\w+")))
- #[1] 3
- Reduce(`intersect`,regmatches(vec1,gregexpr("\w+", vec1)))
- #[1] "Roy" "travels" "Africa"
- a <- "Roy lives in Japan and travels to Africa"
- b <- "Roy travels Africa with this wife"
- c <- "Bob also travels Africa for trips but lives in the US unlike Roy."
- library(stringi);library(qdapTools)
- X <- stri_extract_all_words(list(a, b, c))
- X <- mtabulate(X) > 0
- Y <- colSums(X) == nrow(X); names(Y)[Y]
- [1] "Africa" "Roy" "travels"
Add Comment
Please, Sign In to add comment