[R] how to count occurrences of string?
Daniel Malter
daniel at umd.edu
Sun Sep 20 19:55:38 CEST 2009
Here is one approach: (Some steps are intermediary steps to illustrate what
is going on)
#create sample strings
#repeat each of them 10 times
string=rep(c(
"Lorem ipsum dolor sit amet",
"Lorem ipsum, and lorem ipsum something else",
"Foo",
"Bar",
"Foobar"),10)
##Create a gender variable
gender=rep(0:1,each=25)
##Create an index variable
index=1:length(string)
##This is the string you want to look for
i.am.looking.for="Lorem ipsum"
##Return the indices of the strings
##in which "lorem ipsum" is found?
grep(i.am.looking.for,string)
##Return a boolean for each index
##whether "lorem ipsum" is found or not?
index%in%grep(i.am.looking.for,string)
##Finally, table the occurrence/absence of
##"lorem ipsum" by gender
table(index%in%grep(i.am.looking.for,string),gender)
Hope that helps,
Daniel
-------------------------
cuncta stricte discussurus
-------------------------
-----Ursprüngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Auftrag von w_poet
Gesendet: Sunday, September 20, 2009 11:58 AM
An: r-help at r-project.org
Betreff: [R] how to count occurrences of string?
Hi everyone,
I have an array with a variable called comments. I wish to count the number
of subjects whose post-experiment comments contain a string x and then cross
tab this with other variables of interest, ex. age. Does anyone have any
suggestions on how to count unique occurrences of a string embedded within a
variable and cross-tab it with other variables?
I've looked through the help files, etc. One method I tried was to use
strsplit to split comments. I can use this with table to count the number of
occurrences of x, but I can't cross tab this with other variables because
comments is a ragged array after being split.
Thanks in advance,
Stephen
--
View this message in context:
http://www.nabble.com/how-to-count-occurrences-of-string--tp25530083p2553008
3.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at r-project.org mailing list
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.
More information about the R-help
mailing list