2021年4月4日星期日

select single value based on multiple attributes/col values in data frame in r

This should be a common question, but I was only able find an old question with a complicated answer here. Say I have a table for flight delays and I want to select specific delay time based on attributes like direction and week?

df<- data.frame(    Quarter = paste0("Q", rep(1:4, each = 4)),    Week = rep(c("Weekday", "Weekend"), each = 2, times = 4),    Direction = rep(c("Inbound", "Outbound"), times = 8),    Delay = c(10.8, 9.7, 15.5, 10.3, 11.8, 8.9, 5.5,              3.3, 10.6, 8.8, 6.6, 5.2, 9.1, 7.3, 5.3, 4.4)  )  

A proposed answer of the above post look like this:df[df[,"Quarter"]=="Q1" & df[,"Direction"]=="Inbound","Delay"]. Is there a better way to do this without repeating the subset operations, something simpler in tidyverse?

https://stackoverflow.com/questions/66947791/select-single-value-based-on-multiple-attributes-col-values-in-data-frame-in-r April 05, 2021 at 11:05AM

没有评论:

发表评论