I have the following dataframe:
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 way to find the corresponding price
of the maximum sales
as well as its row number
and record them as a
and b
.
For instance here maximum sales
is equal to 45. This is when the price
is equal to 10 (a should be 10) and this record is on the 4th row (b should be 4).
> a
[1] 10
> b
[1] 4
you can try
(x <- df[df$sales == max(df$sales), ])
sales price
4 45 10
a <- x$price
b <- strtoi(row.names(x))
a <- df[which.max(df$sales),][,2]
b <- order(df[,1],decreasing=T)[1]
a
[1] 10
b
[1] 4
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.