Thanks a lot to you both. Both solutions work great, and thanks to will for explaining how this works. I will have a look into while and if statements in R tomorrow...
2013/9/18 William Dunlap [via R] <ml-node+s789695n4676466...@n4.nabble.com> > > If I have this: > > > > "names" <- c("John", "Jim", "Mary", "Susan") > > "age" <- c(16, 25, 32, 56) > > "income" <- c(2000, 3000, 2500, 1500) > > "all"<- data.frame(names, age, income) > > First, things will be easier for you if you make that dataset as > all <- data.frame( > names = c("John", "Jim", "Mary", "Susan"), > age = c(16, 25, 32, 56), > income = c(2000, 3000, 2500, 1500)) > so you don't have two things called "names", etc., one in the data.frame > and one in the current environment. > > You can select subsets in R using the "[" operator. If it is given an > integer > argument it gives you the items indexed by that that integer vector; if > given a logical argument it gives you the items corresponding to TRUE's > in that logical vector. E.g., try > x <- c(11,22,33,44) > x[c(1,3)] # gives 11 and 33 > x[c(TRUE, FALSE, TRUE, FALSE)] # also gives 11 and 33 > > Make a logical vector of showing which items in 'income' are equal to > its maximum with > atMaxIncome <- max(all$income) == all$income # gives, FALSE TRUE FALSE > FALSE > and do the selection with > all[ atMaxIncome, ] > > > I tried some if-statements, but they didn't work because my programming > > skills outside of SQL are basically non-existent. > > All of this is in Chapter 2 of "An Introduction to R" (about 4 pages into > it), which comes with R. > Read it and do the examples and your R programming skills will improve. > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > -----Original Message----- > > From: [hidden > > email]<http://user/SendEmail.jtp?type=node&node=4676466&i=0>[mailto:[hidden > email] <http://user/SendEmail.jtp?type=node&node=4676466&i=1>] On Behalf > > Of Hal_V > > Sent: Wednesday, September 18, 2013 12:43 PM > > To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4676466&i=2> > > Subject: [R] How to find values that correspond to a given value (i.e. > max) > > > > Hi everyone > > I'm new to R, so this is probably a stupid question, but I looked around > for > > quite a while an couldn't find an answer. Basically I'm trying to print > > values that correspond to a found maximum. > > > > If I have this: > > > > "names" <- c("John", "Jim", "Mary", "Susan") > > "age" <- c(16, 25, 32, 56) > > "income" <- c(2000, 3000, 2500, 1500) > > "all"<- data.frame(names, age, income) > > max(all$income) > > > > I would like to print the name and age that correspond to the found > maximum. > > I tried some if-statements, but they didn't work because my programming > > skills outside of SQL are basically non-existent. > > > > I'd be glad for any pointers, thanks > > > > > > > > -- > > View this message in context: > http://r.789695.n4.nabble.com/How-to-find-values-that- > > correspond-to-a-given-value-i-e-max-tp4676456.html > > Sent from the R help mailing list archive at Nabble.com. > > > > ______________________________________________ > > [hidden email] > > <http://user/SendEmail.jtp?type=node&node=4676466&i=3>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. > > ______________________________________________ > [hidden email] <http://user/SendEmail.jtp?type=node&node=4676466&i=4>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. > > > ------------------------------ > If you reply to this email, your message will be added to the discussion > below: > > http://r.789695.n4.nabble.com/How-to-find-values-that-correspond-to-a-given-value-i-e-max-tp4676456p4676466.html > To unsubscribe from How to find values that correspond to a given value > (i.e. max), click > here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4676456&code=VGltLlVtYmFjaEBodWZ3LmRlfDQ2NzY0NTZ8MTg1MTc5NTEyOQ==> > . > NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> > -- View this message in context: http://r.789695.n4.nabble.com/How-to-find-values-that-correspond-to-a-given-value-i-e-max-tp4676456p4676467.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] ______________________________________________ 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.