Say I have a list: [[1]] "I like google" [[2]] "Hi Google google" [[3]] "what's up" and they are tweets. And I want to find out how many tweets mention google (the answer should be 2). If I string split and unlist them, then I would get the answer of 3. How do I make sure I get just 2? -- View this message in context: http://r.789695.n4.nabble.com/matching-strings-in-a-list-tp4709967.html Sent from the R help mailing list archive at Nabble.com.
> On Jul 16, 2015, at 12:40 PM, tryingtolearn <inshique at ymail.com> wrote: > > Say I have a list: > [[1]] "I like google" > [[2]] "Hi Google google" > [[3]] "what's up" > > and they are tweets. And I want to find out how many tweets mention google > (the answer should be 2). > If I string split and unlist them, then I would get the answer of 3. How do > I make sure I get just 2?See ?grepl presuming that you just want a count of the matches. If you want it to also be case insensitive, set 'ignore.case = TRUE?. For example: Tweets <- list("I like google", "Hi Google google", "what's up?)> Tweets[[1]] [1] "I like google" [[2]] [1] "Hi Google google" [[3]] [1] "what's up?> sapply(Tweets, function(x) grepl("google", x, ignore.case = TRUE))[1] TRUE TRUE FALSE> sum(sapply(Tweets, function(x) grepl("google", x, ignore.case = TRUE)))[1] 2 Regards, Marc Schwartz
Why would you strsplit them? I would think length(grep("google", unlist(x), ignore.case = TRUE)) should do it. Best, Ista On Thu, Jul 16, 2015 at 1:40 PM, tryingtolearn <inshique at ymail.com> wrote:> Say I have a list: > [[1]] "I like google" > [[2]] "Hi Google google" > [[3]] "what's up" > > and they are tweets. And I want to find out how many tweets mention google > (the answer should be 2). > If I string split and unlist them, then I would get the answer of 3. How do > I make sure I get just 2? > > > > -- > View this message in context: http://r.789695.n4.nabble.com/matching-strings-in-a-list-tp4709967.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Thu, Jul 16, 2015 at 12:40 PM, tryingtolearn <inshique at ymail.com> wrote:> Say I have a list: > [[1]] "I like google" > [[2]] "Hi Google google" > [[3]] "what's up" > > and they are tweets. And I want to find out how many tweets mention google > (the answer should be 2). > If I string split and unlist them, then I would get the answer of 3. How do > I make sure I get just 2? >?NROW(grep("google",list,ignore.case=TRUE))? -- Schrodinger's backup: The condition of any backup is unknown until a restore is attempted. Yoda of Borg, we are. Futile, resistance is, yes. Assimilated, you will be. He's about as useful as a wax frying pan. 10 to the 12th power microphones = 1 Megaphone Maranatha! <>< John McKown [[alternative HTML version deleted]]
On Thu, Jul 16, 2015 at 1:00 PM, John McKown <john.archie.mckown at gmail.com> wrote:> On Thu, Jul 16, 2015 at 12:40 PM, tryingtolearn <inshique at ymail.com> > wrote: > >> Say I have a list: >> [[1]] "I like google" >> [[2]] "Hi Google google" >> [[3]] "what's up" >> >> and they are tweets. And I want to find out how many tweets mention google >> (the answer should be 2). >> If I string split and unlist them, then I would get the answer of 3. How >> do >> I make sure I get just 2? >> > > ?NROW(grep("google",list,ignore.case=TRUE))? >?Or sum(grepl("google",x,ignore.case=TRUE)) -- Schrodinger's backup: The condition of any backup is unknown until a restore is attempted. Yoda of Borg, we are. Futile, resistance is, yes. Assimilated, you will be. He's about as useful as a wax frying pan. 10 to the 12th power microphones = 1 Megaphone Maranatha! <>< John McKown [[alternative HTML version deleted]]