[R] substring comparison
claus.orourke at gmail.com
Thu Apr 29 20:07:39 CEST 2010
Thanks. It works perfectly.
On Thu, Apr 29, 2010 at 6:24 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> Try with grepl:
> data$ContainsThe <- ifelse(grepl("the",data$Utt),"y","n")
> On Thu, Apr 29, 2010 at 2:17 PM, Claus O'Rourke <claus.orourke at gmail.com>
>> Hi all,
>> I'm writing a script to do some basic text analysis in R. Let's assume
>> I have a data frame named data which contains a column named 'utt'
>> which contains strings. Is there a straightforward way to achieve
>> something like this:
>> data$ContainsThe <- ifelse(startsWith(data$Utt,"the"),"y","n")
>> data$ContainsThe <- ifelse(contains(data$Utt,"the"),"y","n")
>> I tried using grep
>> data$ContainsThe <- ifelse(grep("the",data$Utt),"y","n")
>> but this doesn't work becausee grep only returns the rows for which
>> grep succeeded.
>> Thanks for any pointers
>> R-help at r-project.org mailing list
>> PLEASE do read the posting guide
>> and provide commented, minimal, self-contained, reproducible code.
> Henrique Dallazuanna
> 25° 25' 40" S 49° 16' 22" O
More information about the R-help