[R] Using grep() to subset lines of text
Uwe Ligges
ligges at statistik.tu-dortmund.de
Tue Dec 2 10:39:27 CET 2008
ppaarrkk wrote:
> I have two vectors, a and b. b is a text file. I want to find in b those
> elements of a which occur at the beginning of the line in b. I have the
> following code, but it only returns a value for the first value in a, but I
> want both. Any ideas please.
>
>
> a = c(2,3)
>
> b = NULL
> b[1] = "aaa 2 aaa"
> b[2] = "2 aaa"
> b[3] = "3 aaa"
> b[4] = "aaa 3 aaa"
>
> grep(paste("^",a, sep=""), b )
>
grep(paste("^", a, collapse = "|", sep = ""), b)
Uwe Ligges
More information about the R-help
mailing list