[R] grep or other complex string matching approach to capture necessary information...

David Winsemius dwinsemius at comcast.net
Sat Sep 26 18:36:57 CEST 2009


On Sep 26, 2009, at 11:40 AM, John Kane wrote:

> ?subset
>
> problems <- c( "Water damage",  "Water off", "water pipes damaged",  
> "leaking water")
>
> damaged <- subset(house_info, house_info[,1]==problems[1]
>                    | house_info[,1]==problems[2]
>                      | house_info[,1]==problems[3]
>                       | house_info[,1]==problems[4])
>
> or am I misunderstanding the question?
>
> or perhaps %in% which probably does the job more elegantly but I  
> forget the syntax at the moment.

 > problems <- c( "Water damage",  "Water off", "water pipes damaged",  
"leaking water")
 > damaged <- subset(house_info, house_info[,1] %in% problems)
 > str(damaged)
'data.frame':	49 obs. of  2 variables:
  $ water_evaluation.water_evaluation_selection.: Factor w/ 5 levels  
"No water damage",..: 5 3 5 2 5 3 5 3 5 5 ...
  $ house_number                                : num  276 594 591 376  
229 428 248 237 534 517 ...

>
> --- On Fri, 9/25/09, Jason Rupert <jasonkrupert at yahoo.com> wrote:
>
>> From: Jason Rupert <jasonkrupert at yahoo.com>
>> Subject: [R] grep or other complex string matching approach to  
>> capture necessary information...
>> To: R-help at r-project.org
>> Received: Friday, September 25, 2009, 1:58 PM
>> Say I have the following data:
>>
>>
>> house_number<-floor(runif(100, 200, 600))
>> water_evaluation<-c("No water damage", "Water damage",
>> "Water On", "Water off", "water pipes damaged", "leaking
>> water")
>> water_evaluation_selection<-floor(runif(100, 1,6))
>> house_info<-data.frame(water_evaluation[water_evaluation_selection],
>>
>>        house_number)
>>
>> And, that I only want to pull out the ones with negative
>> water evaluations, i.e. Water damage, water pipes damaged,
>> and leaking water.
>>
>> Should/could I use grep in order to pull the house numbers
>> out of house_info with those negative water
>> evaluations?
>>
>> I guess I want to know the house numbers from house_info
>> where the water evaluation is negative.  Is there a way
>> to use grep or another R function in order to acquire that
>> information?
>>
>> Thank you again in advance for any insights.
>>
>> ______________________________________________
>> R-help at r-project.org
>> 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.
>>
>
>
>       
> __________________________________________________________________
> Be smarter than spam. See how smart SpamGuard is at giving junk  
> email the boot with the All
> ______________________________________________
> R-help at r-project.org 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list