[R] Searching a Table

Jason Turner jasont at indigoindustrial.co.nz
Fri Oct 24 07:13:09 CEST 2003


Nicole Baggette wrote:

> Is there any way to search a column of a table?
> 
>  
> 
> I read in a table and I want to create a subset based on the criteria that a
> certain column (filled with words) has a word which starts with a certain
> letter. The only method I have seen that searches this way is apropos(), but
> that doesn't seem to search the location I want. If anyone can make a
> recommendation I would appreciate it.

help(grep)

To search for words that start with "W" or "w" (minus the quotes) in a 
data frame called "mydata", column 3,

grep("\\bw", mydata[,3], ignore.case=TRUE, perl=TRUE)

Which means - "\\b" - word boundary.
The "w" and ignore.case should be self-explanatory.
perl - use perl style regular expressions.

example:

foo <- c("now is The Time","for all good llamas","to come to the party")
foo

#find lines that contain words starting with "t" or "T"

grep("\\bt",foo,ignore.case=TRUE,perl=TRUE)

foo[grep("\\bt",foo,ignore.case=TRUE,perl=TRUE)]

Cheers

Jason
-- 
Indigo Industrial Controls Ltd.
http://www.indigoindustrial.co.nz
64-21-343-545
jasont at indigoindustrial.co.nz




More information about the R-help mailing list