Hi, I have a data.frame and I need to know witch store sold more "Lapiseiras". I got a point where I am almost sure I am very closed to the answer but I am missing somethiing. So, the data frame is this one dados<-data.frame(Store=c("Setubal","lx","Aveiro","Coimbra","Aveiro","Evora","Aveiro","Coimbra","Setubal"),Prod=c("Bloco Desenho","Bloco Desenho","Tinteiro","R?gua","Lapiseira","Regua","Tinteiro","Lapiseira","Lapiseira"),qtd=c(2,1,2,1,3,1,1,1,1)) then I do this: dados[dados$Prod=="Lapiseira",c("Store","qtd")] and I get this: Store qtd 5 Aveiro 3 8 Coimbra 1 9 Setubal 1 but the answer I want to obtain is: "Aveiro" can you help me please? thanks AD -- View this message in context: http://r.789695.n4.nabble.com/Help-in-getting-info-from-a-DataFrame-tp3247740p3247740.html Sent from the R help mailing list archive at Nabble.com.
On Jan 30, 2011, at 5:27 PM, ADias wrote:> dados<- > data > .frame > (Store > = > c > ("Setubal > ","lx > ","Aveiro > ","Coimbra > ","Aveiro","Evora","Aveiro","Coimbra","Setubal"),Prod=c("Bloco > Desenho","Bloco > Desenho > ","Tinteiro > ","R?gua > ","Lapiseira > ","Regua > ","Tinteiro","Lapiseira","Lapiseira"),qtd=c(2,1,2,1,3,1,1,1,1))See if this is a more useful approach: > lapsales <- dados[dados$Prod=="Lapiseira", ] > lapsales[which.max(lapsales[ , "qtd" ]), "Store"] [1] Aveiro Levels: Aveiro Coimbra Evora lx Setubal -- David Winsemius, MD West Hartford, CT
David Winsemius wrote:> > > On Jan 30, 2011, at 5:27 PM, ADias wrote: > >> dados<- >> data >> .frame >> (Store >> = >> c >> ("Setubal >> ","lx >> ","Aveiro >> ","Coimbra >> ","Aveiro","Evora","Aveiro","Coimbra","Setubal"),Prod=c("Bloco >> Desenho","Bloco >> Desenho >> ","Tinteiro >> ","R?gua >> ","Lapiseira >> ","Regua >> ","Tinteiro","Lapiseira","Lapiseira"),qtd=c(2,1,2,1,3,1,1,1,1)) > > > See if this is a more useful approach: > > > lapsales <- dados[dados$Prod=="Lapiseira", ] > > lapsales[which.max(lapsales[ , "qtd" ]), "Store"] > [1] Aveiro > Levels: Aveiro Coimbra Evora lx Setubal > > -- > David Winsemius, MD > West Hartford, CT > > ______________________________________________ > 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. > >Hi, thank you for the help. Two questions I would like to make. Suppose I have put your answer on an object like this: r<-lapsales[which.max(lapsales[ , "qtd" ]), "Store"] so>r[1] Aveiro Levels: Aveiro Coimbra Evora lx Setubal and suppoese I wish to add this year result to previous years results: results<-c("Lx","Setubal") so I did: results<-c(results,r) and what I get is:> results[1] "Lx" "Setubal" "1" why "1" and not "Aveiro"? Second question is:> lapsalesStore Prod qtd 5 Aveiro Lapiseira 3 8 Coimbra Lapiseira 1 9 Setubal Lapiseira 1> lapsales[max(lapsales$qtd),"Store"][1] Setubal Levels: Aveiro Coimbra Evora lx Setubal why Setubal and not Aveiro? many thanks AD -- View this message in context: http://r.789695.n4.nabble.com/Help-in-getting-info-from-a-DataFrame-tp3247740p3248276.html Sent from the R help mailing list archive at Nabble.com.
Hi r-help-bounces at r-project.org napsal dne 31.01.2011 09:44:00:> > > David Winsemius wrote: > > > > > > On Jan 30, 2011, at 5:27 PM, ADias wrote: > > > >> dados<- > >> data > >> .frame > >> (Store > >> = > >> c > >> ("Setubal > >> ","lx > >> ","Aveiro > >> ","Coimbra > >> ","Aveiro","Evora","Aveiro","Coimbra","Setubal"),Prod=c("Bloco > >> Desenho","Bloco > >> Desenho > >> ","Tinteiro > >> ","R?gua > >> ","Lapiseira > >> ","Regua > >> ","Tinteiro","Lapiseira","Lapiseira"),qtd=c(2,1,2,1,3,1,1,1,1)) > > > > > > See if this is a more useful approach: > > > > > lapsales <- dados[dados$Prod=="Lapiseira", ] > > > lapsales[which.max(lapsales[ , "qtd" ]), "Store"] > > [1] Aveiro > > Levels: Aveiro Coimbra Evora lx Setubal > > > > -- > > David Winsemius, MD > > West Hartford, CT > > > > ______________________________________________ > > 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. > > > > > Hi, > > thank you for the help. > > Two questions I would like to make. > > Suppose I have put your answer on an object like this: > > r<-lapsales[which.max(lapsales[ , "qtd" ]), "Store"] > > so > >r > [1] Aveiro > Levels: Aveiro Coimbra Evora lx Setubal > > and suppoese I wish to add this year result to previous years results: > > results<-c("Lx","Setubal") > so I did: > > results<-c(results,r) > > and what I get is: > > > results > [1] "Lx" "Setubal" "1" > > why "1" and not "Aveiro"?you has to distinct between factor and character objects. Factor is vector of integers with labels and concatenation add this integer but not a label. Transform factor to character vector. results<-c(results,as.character(r)) Regards Petr> > Second question is: > > > lapsales > Store Prod qtd > 5 Aveiro Lapiseira 3 > 8 Coimbra Lapiseira 1 > 9 Setubal Lapiseira 1 > > lapsales[max(lapsales$qtd),"Store"] > [1] Setubal > Levels: Aveiro Coimbra Evora lx Setubal > > why Setubal and not Aveiro? > > many thanks > AD > -- > View this message in context:http://r.789695.n4.nabble.com/Help-in-getting-> info-from-a-DataFrame-tp3247740p3248276.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
On Jan 31, 2011, at 3:44 AM, ADias wrote:> > > David Winsemius wrote: >> >> >> On Jan 30, 2011, at 5:27 PM, ADias wrote: >> >>> dados<- >>> data >>> .frame >>> (Store >>> >>> c >>> ("Setubal >>> ","lx >>> ","Aveiro >>> ","Coimbra >>> ","Aveiro","Evora","Aveiro","Coimbra","Setubal"),Prod=c("Bloco >>> Desenho","Bloco >>> Desenho >>> ","Tinteiro >>> ","R?gua >>> ","Lapiseira >>> ","Regua >>> ","Tinteiro","Lapiseira","Lapiseira"),qtd=c(2,1,2,1,3,1,1,1,1)) >> >> >> See if this is a more useful approach: >> >>> lapsales <- dados[dados$Prod=="Lapiseira", ] >>> lapsales[which.max(lapsales[ , "qtd" ]), "Store"] >> [1] Aveiro >> Levels: Aveiro Coimbra Evora lx Setubal >> >> -- >> David Winsemius, MD >> West Hartford, CT >> >> ______________________________________________ >> 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. >> >> > Hi, > > thank you for the help. > > Two questions I would like to make. > > Suppose I have put your answer on an object like this: > > r<-lapsales[which.max(lapsales[ , "qtd" ]), "Store"] > > so >> r > [1] Aveiro > Levels: Aveiro Coimbra Evora lx Setubal > > and suppoese I wish to add this year result to previous years results: > > results<-c("Lx","Setubal") > so I did: > > results<-c(results,r) > > and what I get is: > >> results > [1] "Lx" "Setubal" "1"Right. You appended a factor variable to a character variable and got the numeric representation for the Aveiro values which is 1 and it then got coerced into "1" because the firts element offered to the c() function was character.> > > why "1" and not "Aveiro"? > > Second question is: > >> lapsales > Store Prod qtd > 5 Aveiro Lapiseira 3 > 8 Coimbra Lapiseira 1 > 9 Setubal Lapiseira 1 >> lapsales[max(lapsales$qtd),"Store"] > [1] Setubal > Levels: Aveiro Coimbra Evora lx SetubalThe max of lapsales$qtd is 3 to it is looking up the third store == Setubal. -- David.> > why Setubal and not Aveiro? > > many thanks > AD > -- > View this message in context: http://r.789695.n4.nabble.com/Help-in-getting-info-from-a-DataFrame-tp3247740p3248276.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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 West Hartford, CT