[R] grepping out columns

Kate Ignatius kate.ignatius at gmail.com
Thu Feb 19 04:33:48 CET 2015


Thanks!  That was helpful.  Although I think there was a typo in the last line:

selected <- sort(unique(unlist(all_ind)))

but I figured it out :)

K.

On Wed, Feb 18, 2015 at 4:10 PM, Federico Lasa <felasa at gmail.com> wrote:
> David's almost works except it catches the "MONTH" column, just add an
> empty metacharacter tho.
>
> c("DAY",
> "MONTH",
> "YEAR",
> "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")-> columns
>
> sa_ind <- grep("SA_",columns)
> days <- gsub("SA_","", columns[sa_ind])
> days <- paste0(days,"$")
> selected <- lapply(days, function(x) grep(x,columns))
> selected <- sort(unique(unlist(all_ind)))
>
> columns[selected]
> [1] "SA_TUES" "SA_MON"  "CH_TUES" "CH_MON"  "AR_TUES" "AR_MON"
> "SA_THUR" "CH_THUR" "AR_THUR"
>
> On Wed, Feb 18, 2015 at 2:55 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>>
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.



More information about the R-help mailing list