Hi all, I have not been able to find an answer to what is a simple question: conaguaMexicoSub <- subset(conagua, unidad == "Jalapa", select = c(equipo,X101:X309)) This subset gives me all the rows from conagua where unidad is Jalapa. But, please, how do I get all the rows where unidad contents Jalapa? I've been looking at regex, but I didn't find how pass one to unidad ==. Thanks! -- Ricardo Rodr?guez Your XEN ICT Team
On Tue, 15 Apr 2008, [Ricardo Rodriguez] Your XEN ICT Team wrote:> Hi all, > > I have not been able to find an answer to what is a simple question: > > conaguaMexicoSub <- subset(conagua, unidad == "Jalapa", select > c(equipo,X101:X309)) > > This subset gives me all the rows from conagua where unidad is Jalapa. > But, please, how do I get all the rows where unidad contents Jalapa? > > I've been looking at regex, but I didn't find how pass one to unidad ==.You want ?regexpr Something like regexpr("Jalapa", as.character( unidad ) ) != -1 HTH, Chuck> > Thanks! > > -- > Ricardo Rodr?guez > Your XEN ICT Team > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Try something like this: x[grep("Jalapa", x$unidad),, drop = F] On Mon, Apr 14, 2008 at 9:10 PM, [Ricardo Rodriguez] Your XEN ICT Team < webmaster@xen.net> wrote:> Hi all, > > I have not been able to find an answer to what is a simple question: > > conaguaMexicoSub <- subset(conagua, unidad == "Jalapa", select > c(equipo,X101:X309)) > > This subset gives me all the rows from conagua where unidad is Jalapa. > But, please, how do I get all the rows where unidad contents Jalapa? > > I've been looking at regex, but I didn't find how pass one to unidad ==. > > Thanks! > > -- > Ricardo Rodríguez > Your XEN ICT Team > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Thanks all, markleeds at verizon.net wrote:>> From: "[Ricardo Rodriguez] Your XEN ICT Team" <webmaster at xen.net> >> Date: 2008/04/14 Mon PM 07:43:23 CDT >> To: Henrique Dallazuanna <wwwhsd at gmail.com>, r-help at r-project.org >> Subject: Re: [R] looking for a string >> > > henrique means to use his command directly and > forget about subset. subset probably doesn't > let you do what you want to do. > >Mark, I've finally found an example that helps me to understand what Henrique said. Here it is: ## sometimes requiring a logical 'subset' argument is a nuisance nm <- rownames(state.x77) start_with_M <- nm %in% grep("^M", nm, value=TRUE) subset(state.x77, start_with_M, Illiteracy:Murder) Coming from http://wiki.r-project.org/rwiki/doku.php?id=rdoc:base:subset What I am not able to understand yet is Charles's proposal. Any insight will be welcome! Cheers, Ricardo -- Ricardo Rodr?guez Your XEN ICT Team
Thanks Mark! markleeds at verizon.net wrote:>> From: "[Ricardo Rodriguez] Your XEN ICT Team" <webmaster at xen.net> >> Date: 2008/04/15 Tue PM 04:48:12 CDT >> To: markleeds at verizon.net, r-help at r-project.org >> Subject: Re: [R] looking for a string >> > > Hi Ricardo: you can use the idea you > found below to apply Henrique's code inside subset. > that's fine. > > BUT, you can also use Henrique's > code directly without using subset. it should > work as is. test it out. i'm pretty sure it should. > > ALso, I don't remember what Charle's proposal was but, > if you send it to me, I can see if I understand > it and then explain it to you if i do. > > I have to leave now but I'll log in again around > 11 pm US time. good luck.Yeah, I think I got grep idea. Both options are possible. Charles's entry: regexpr("Jalapa", as.character( unidad ) ) != -1 It was the first reply to my message. It is a bit late here in Europe, so tomorrow morning I will keep track of this thread. Any insight will be welcome! Thank you so much, Ricardo -- Ricardo Rodr?guez Your XEN ICT Team
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
Mark Leeds wrote:> Hi Ricardo: I'm glad it helped but check > > regexpr("Jalapa", as.character( unidad ) ) != -1 > > because I think it should be > > regexpr("Jalapa", as.character( DF$unidad ) ) != -1 > > if unidad is a column of DF.Checked! attach(DF) did the trick! :-) Greetings, Ricardo -- Ricardo Rodr?guez Your XEN ICT Team