[R] Counting the occurences of a charater within a string

Douglas Esneault Douglas.Esneault at mecglobal.com
Thu Dec 1 17:32:30 CET 2011


I am new to R but am experienced SAS user and I was hoping to get some help on counting the occurrences of a character within a string at a row level.

My dataframe, x,  is structured as below:

Col1
abc/def
ghi/jkl/mno

I found this code on the board but it counts all occurrences of "/" in the dataframe.  

chr.pos <- which(unlist(strsplit(x,NULL))=='/')
chr.count <- length(chr.pos)
chr.count
[1] 3

I'd like to append a column, say cnt, that has the count of "/" for each row.

Can anyone point me in the right direction or offer some code to do this?

Thanks in advance for the help.

Doug Esneault






Privileged/Confidential Information may be contained in this message. If you
are not the addressee indicated in this message (or responsible for delivery
of the message to such person), you may not copy or deliver this message to 
anyone. In such case, you should destroy this message and kindly notify the
sender by reply email. Please advise immediately if you or your employer
does not consent to email for messages of this kind. Opinions, conclusions
and other information in this message that do not relate to the official
business of the GroupM companies shall be understood as neither given nor
endorsed by it.   GroupM companies are a member of WPP plc. For more
information on our business ethical standards and Corporate Responsibility 
policies please refer to our website at
http://www.wpp.com/WPP/About/




More information about the R-help mailing list