grep for strings

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

grep for strings

Santosh Srinivas
I am trying to find the function where I can search for a pattern in a
text string (I thought I could use grep for this but no :().

> x
[1] "abcdefghijkl"

I want to find the positions (i.e. equivalent of nchar) for "cd" and
in case there are multiple hits .. then the results as a array

Thank you.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: grep for strings

cberry
On Sun, 5 Dec 2010, Santosh Srinivas wrote:

> I am trying to find the function where I can search for a pattern in a
> text string (I thought I could use grep for this but no :().

Correct, but reading

  ?grep

and running

  example( grep )

should show you how to do that search.

>
>> x
> [1] "abcdefghijkl"
>
> I want to find the positions (i.e. equivalent of nchar) for "cd" and
> in case there are multiple hits .. then the results as a array

You will need to deal with the possibility that there are more 'cd's in
some elements of 'text' than in others.

So, it is not obvious that an _array_ will work without some special
tooling.

HTH,

Chuck



>
> Thank you.
>
> ______________________________________________
> [hidden email] 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.
>

Charles C. Berry                            Dept of Family/Preventive Medicine
[hidden email]    UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: grep for strings

jholtman
In reply to this post by Santosh Srinivas
> x <- "abcdefghijkl"
> ?regexpr
starting httpd help server ... done
> regexpr('cd', x)
[1] 3
attr(,"match.length")
[1] 2
>


On Sat, Dec 4, 2010 at 9:34 PM, Santosh Srinivas
<[hidden email]> wrote:

> I am trying to find the function where I can search for a pattern in a
> text string (I thought I could use grep for this but no :().
>
>> x
> [1] "abcdefghijkl"
>
> I want to find the positions (i.e. equivalent of nchar) for "cd" and
> in case there are multiple hits .. then the results as a array
>
> Thank you.
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: grep for strings

Petr Savicky
In reply to this post by Santosh Srinivas
On Sun, Dec 05, 2010 at 08:04:08AM +0530, Santosh Srinivas wrote:
> I am trying to find the function where I can search for a pattern in a
> text string (I thought I could use grep for this but no :().
>
> > x
> [1] "abcdefghijkl"
>
> I want to find the positions (i.e. equivalent of nchar) for "cd" and
> in case there are multiple hits .. then the results as a array

For a single string, for example

  p <- gregexpr("cd", "abcdecdecdcd")[[1]]

  p
  [1]  3  6  9 11
  attr(,"match.length")
  [1] 2 2 2 2

  as.numeric(p) # [1]  3  6  9 11

For a vector of strings, for example

  p <-  gregexpr("cd", c("abcde", "acdecde", "abcdecdecdcd", "cdcd"))
  m <- max(unlist(lapply(p, length)))
  out <- matrix(nrow=length(p), ncol=m)
  for (i in seq(nrow(out))) {
      out[i, seq(length(p[[i]]))] <- p[[i]]
  }

  out
       [,1] [,2] [,3] [,4]
  [1,]    3   NA   NA   NA
  [2,]    2    5   NA   NA
  [3,]    3    6    9   11
  [4,]    1    3   NA   NA

Petr Savicky.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: grep for strings

sayan dasgupta
Hi Santosh,


you may also try

require(stringr)
x <- "abcdefghijklcd"
str_locate_all(x,"cd")


On Sun, Dec 5, 2010 at 1:21 PM, Petr Savicky <[hidden email]> wrote:

> On Sun, Dec 05, 2010 at 08:04:08AM +0530, Santosh Srinivas wrote:
> > I am trying to find the function where I can search for a pattern in a
> > text string (I thought I could use grep for this but no :().
> >
> > > x
> > [1] "abcdefghijkl"
> >
> > I want to find the positions (i.e. equivalent of nchar) for "cd" and
> > in case there are multiple hits .. then the results as a array
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.