[R] How to find values that correspond to a given value (i.e. max)

William Dunlap wdunlap at tibco.com
Wed Sep 18 23:51:02 CEST 2013


> 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: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Hal_V
> Sent: Wednesday, September 18, 2013 12:43 PM
> To: r-help at r-project.org
> 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.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list