简体   繁体   中英

How to know the summary of the distance with ends_with selection

im trying to get the summary of the distance with the flights ends with INC text

so i did join two database to get names

flights <- left_join(flights, airlines, by="carrier")

than i used function:

> flights %>% select(name, ends_with("Inc.")) %>% summarise(dist=sum(flights$distance))
# A tibble: 1 x 1
       dist
      <dbl>
1 350217607

and also tried:

> flights %>% filter(name, ends_with("Inc.")) %>% summarise(dist=sum(flights$distance))
Error: No tidyselect variables were registered
Call `rlang::last_error()` to see a backtrace

But in first case its simply making summary of all airlines not the one i specified shall be finished with "Inc." second trial simply says mistake etc... what am i doing wrong?

Thank you

You could do this in multiple ways, some of them as shown below

library(dplyr)
flights %>% filter(grepl("Inc.$", name)) %>% summarise(dist = sum(distance))

#       dist
#      <dbl>
#1 249500641

flights %>%  summarise(dist = sum(distance[grepl("Inc.$", name)]))

flights %>% slice(grep("Inc.$", name)) %>% summarise(dist = sum(distance))

Or using base R

sum(with(flights, distance[endsWith(name, "Inc.")]))
#[1] 249500641

sum(with(flights, distance[grepl("Inc.$", name)]))

sum(with(flights, distance[grep("Inc.$", name)]))

Also a side note, Never use $ in pipes more often than not it will mess up the calculation.

We can use tidyvverse methods

library(dplyr)
library(stringr)
flights %>%
     filter(str_detect(name, "Inc\\.$")) %>%
      summarise(dist = sum(distance))

If we use ends_with with select statement, it checks the column names and select the matching column. Here, the OP wants to select rows. So, the pattern should be used with filter on the selected column name

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM