[R] grepping out columns

David Winsemius dwinsemius at comcast.net
Wed Feb 18 21:55:35 CET 2015


On Feb 18, 2015, at 12:27 PM, Kate Ignatius wrote:
> Hi,
> 
> I've got a complicated grep problem (or not)...  I currently have a
> file with the headings as follows:
> 
Lets assume these values are in a character vector named 'dat'.
> SA_TUES
> SA_MON
> SU_WED
> CH_TUES
> CH_WED
> CH_MON
> AR_TUES
> AR_WED
> AR_MON
> SA_THUR
> SU_FRI
> CH_THUR
> CH_FRI
> AR_THUR
> AR_FRI

 sadays <- dat[grep("SA", dat) ]
 sads <- gsub("SA_","",sadays)
 sads
#[1] "TUES" "MON"  "THUR"

 dat[ sapply(sads, grep, dat) ]
#[1] "SA_TUES" "CH_TUES" "AR_TUES" "SA_MON"  "CH_MON"  "AR_MON" 
#[7] "SA_THUR" "CH_THUR" "AR_THUR

-- 
David Winsemius
Alameda, CA, USA



More information about the R-help mailing list