Home > Net >  How to use min in dataframe
How to use min in dataframe

Time:07-28

How to print the day having minimum wind and the day having maximum temperature from dataset airquality? I tried:

data(airquality)
min(airquality$Wind)

but it just gives me the value 1.7.

Edit:

OK, now I know which.min. I tried:

airquality[which.min(airquality$Wind),]
#Ozone Solar.R Wind Temp Month Day
#53    NA      59  1.7   76     6  22

But I want to print just the two last column Month and Day.

CodePudding user response:

I find the solution:

airquality[,5:6][which.min(airquality$Wind), ]
#   Month Day
#53     6  22

I just find it by trying. Thank you for your help.


Or as Zheyuan Li suggested:

airquality[which.min(airquality$Wind), c("Month", "Day")]
airquality[which.min(airquality$Wind), 5:6]

airquality[which.max(airquality$Wind), c("Month", "Day")]
airquality[which.max(airquality$Wind), 5:6]

airquality[c(which.min(airquality$Wind), which.max(airquality$Wind)), c("Month", "Day")]
airquality[c(which.min(airquality$Wind), which.max(airquality$Wind)), 5:6]

CodePudding user response:

Try

airquality[airquality$Wind == min(airquality$Wind), ]

I think this is more general than which.min because which.min cannot deal with ties.

CodePudding user response:

Another option using subset:

data(airquality)
output <- subset(airquality, Wind == min(Wind))
output[,5:6]
#>    Month Day
#> 53     6  22

Created on 2022-07-27 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related