I created this data frame with station IDs and respective counts. Now I want to find the 10 largest values and the 10 smallest values in this data frame and save them in a new data frame.
station_activity <- tripdata %>% count(start.station.id)
station_activity <- station_activity %>% filter(n > 6100 & n < 100)
Line 1 was used to create the data frame seen in the picture and line 2 was used to filter by the values, but that does obviously result in an empty data frame. Is there a better version to do so, without writing a long conditional code?
CodePudding user response:
The condition should be OR
(|
) and not AND
(&
) as a value can't be both greater than 6100 and less than 100
library(dplyr)
station_activity %>%
filter(n > 6100 | n < 100)
If we want to get the top 10 and bottom 10, arrange
by the 'n' column from the count
, and slice
the first 10 and last 10 rows
tripdata %>%
count(start.station.id) %>%
arrange(n) %>%
slice(c(head(row_number(), 10), tail(row_number(), 10)))