Text processing

15 views
Skip to first unread message

Avner Kantor

unread,
Jul 24, 2014, 7:56:35 AM7/24/14
to israel-r-user-group
Hi all,

Suppose I have: 

myList <- list(a=c("aa", "bb"), b=c("ee", "ffg", "gg", "dd"))

I want to find:
1. Which vector contains "ffg"?
2. Which vector contains string that is substring of "Alaaaaa"?

Thanks,

Avner

amit gal

unread,
Jul 24, 2014, 8:23:06 AM7/24/14
to israel-r-...@googlegroups.com
1) sapply(myList, function(x) "ffg" %in% x)
2) sapply(myList,function(t) {a = sum(sapply(t,grepl,x="Alaaa")); a>0})


number 2 is really awkward and I'm sure there is some text/string analysis packages that has some more user friendly functionality (which will probably also solve 1)




--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

דוד ארנבורג

unread,
Jul 25, 2014, 3:01:17 AM7/25/14
to israel-r-...@googlegroups.com, avner...@gmail.com
@Amit you could slightly improve your answer by 

sapply(myList, function(t) any(sapply(t, grepl, "Alaaa")))

amit gal

unread,
Jul 25, 2014, 3:14:20 AM7/25/14
to israel-r-...@googlegroups.com
yes. thanks. forgot about the any() function.


Message has been deleted

דוד ארנבורג

unread,
Jul 25, 2014, 3:35:22 AM7/25/14
to israel-r-...@googlegroups.com, avner...@gmail.com
Though the best solution (in my opinion) for the second question will be the following (using only one sapply)

sapply(myList, function(x) grepl(paste(x, collapse = "|"), "Alaaa"))


On Thursday, July 24, 2014 2:56:35 PM UTC+3, אבנר קנטור wrote:
Reply all
Reply to author
Forward
0 new messages