Search for matching records of a specific value in a data frame in R

I have the following framework:

 sales<-c(1,2,3,45,4,3,21)
 price<-c(35,45,30,10,33,44,15)
 df<-data.frame(sales,price)

I am looking for a way to find the appropriate price maximum salesone as well as it row numberand write them down like aand b.

For example, the maximum salesis 45. This is when it priceis 10 (a must be 10), and this record is on the 4th line (b must be 4).

  > a
 [1] 10
 > b
 [1] 4
+4
source share
2 answers

you can try

(x <- df[df$sales == max(df$sales), ])
  sales price
4    45    10
a <- x$price
b <- strtoi(row.names(x))
+4
source
a <- df[which.max(df$sales),][,2]
b <- order(df[,1],decreasing=T)[1]
a
[1] 10
b
[1] 4
0
source

All Articles