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

2011-12-03 Thread Hadley Wickham
On Thu, Dec 1, 2011 at 10:32 AM, Douglas Esneault douglas.esnea...@mecglobal.com wrote: 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

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

2011-12-01 Thread Douglas Esneault
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

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

2011-12-01 Thread Bert Gunter
## It's not a data frame -- it's just a vector. x [1] abc/def ghi/jkl/mno gsub([^/],,x) [1] / // nchar(gsub([^/],,x)) [1] 1 2 ?gsub ?nchar -- Bert On Thu, Dec 1, 2011 at 8:32 AM, Douglas Esneault douglas.esnea...@mecglobal.com wrote: I am new to R but am experienced SAS user and I

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

2011-12-01 Thread Florent D.
I used within and vapply: x - data.frame(Col1 = c(abc/def, ghi/jkl/mno), stringsAsFactors = FALSE) count.slashes - function(string)sum(unlist(strsplit(string, NULL)) == /)within(x, Col2 - vapply(Col1, count.slashes, 1))          Col1 Col21     abc/def    12 ghi/jkl/mno    2 On Thu, Dec 1, 2011

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

2011-12-01 Thread Florent D.
Resending my code, not sure why the linebreaks got eaten: x - data.frame(Col1 = c(abc/def, ghi/jkl/mno), stringsAsFactors = FALSE) count.slashes - function(string)sum(unlist(strsplit(string, NULL)) == /) within(x, Col2 - vapply(Col1, count.slashes, 1)) Col1 Col2 1 abc/def1 2

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

2011-12-01 Thread Bert Gunter
strsplit is certainly an alternative, but your approach is unnecessarily complicated and inefficient. Do this, instead: sapply(strsplit(x,/),length)-1 Cheers, Bert On Thu, Dec 1, 2011 at 7:44 PM, Florent D. flo...@gmail.com wrote: Resending my code, not sure why the linebreaks got eaten: x -

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

2011-12-01 Thread Florent D.
Inefficient, maybe, but what you suggest does not work if a string starts or ends with a slash. On Thu, Dec 1, 2011 at 11:11 PM, Bert Gunter gunter.ber...@gene.com wrote: strsplit is certainly an alternative, but your approach is unnecessarily complicated and inefficient. Do this, instead:

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

2011-12-01 Thread David Winsemius
On Dec 1, 2011, at 11:11 PM, Bert Gunter wrote: strsplit is certainly an alternative, but your approach is unnecessarily complicated and inefficient. Do this, instead: sapply(strsplit(x,/),length)-1 Definitely more compact that the regex alternates I came up with, but one of these still