[R] looking for a string

[Ricardo Rodriguez] Your XEN ICT Team webmaster at xen.net
Wed Apr 16 14:01:22 CEST 2008


Thanks Mark!

Mark Leeds wrote:
> Hi Ricardo: you can look at ?regexpr but I'm pretty sure that below returns
> a vector of TRUES and FALSES depending on whether the condition ( is Jalapa
> contained in unidad ) is TRUE. So, wherever the vector is TRUE is where
> Jalapa was contained in unidad. So, it's basically EXACTLY the same
> as what Henrique did. I just can't remember exactly what Henrique did
> at the moment.
>
> regexpr("Jalapa", as.character( unidad ) ) != -1
>
> So, suppose your dataframe was called DF and it has column called unidad.
>
> Then, 
>
> newDF<-DF[(regexpr("Jalapa", as.character( unidad ) ) != -1),]
>
> should be a new dataframe , exactly the same as the old one, but only
> containing the rows where the condition was true. Does that clear things up
> for you ?  

Yes. It is clear for me now. Thanks for your detailed explanation!
> The expression is for the rows and  has the TRUES and FALSES as a
> vector and whenever it is TRUE, the row is kept and whatever is FALSE, the
> row is not kept. The comma just says to take all the columns.
>
> You can also use subset directly if that's easier for you the same way
> you figured out how to use it in the state.77 example.
>
> If it's not clear, it's because you need to understand how logical
> subscripting is used in R. For that, I think the thing to read is
> "Introduction to R" which is one of the documents at www.r-project.org.
>
> Do 
>
>   

Yes, this is the main problem: I need to understand basic concepts. And 
I am aware that logical subscripting is not one of my strengths. The 
example has also forced me to understand another basic concetp: attach() 
and detach()! I didn't know why unidad was not available just by name in 
my environment. I used to call variables as name$dataframe.
> temp<- regexpr("Jalapa", as.character( unidad ) ) != -1
> print(temp) 
>
> to see exactly what regexpr does.

Thanks again.

Cheers,

Ricardo

-- 
Ricardo Rodríguez
Your XEN ICT Team



More information about the R-help mailing list