繁体   English   中英

如何知道ends_with选择的距离汇总

[英]How to know the summary of the distance with ends_with selection

我试图获取与航班的距离的摘要以INC文本结尾

所以我确实加入了两个数据库来获取名称

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

比我使用的功能:

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

并且还尝试了:

> 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

但在第一种情况下,其对所有航空公司的简单总结而不是我指定的摘要应以“ Inc”结尾。 第二次审判只是说错误等...我在做什么错?

谢谢

您可以通过多种方式进行操作,其中一些方法如下所示

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))

或使用基数R

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

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

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

另外请注意,不要在管道中经常使用$ ,否则会弄乱计算。

我们可以使用tidyvverse方法

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

如果我们在select语句中使用ends_with ,它将检查列名称并选择匹配的列。 在这里,OP要选择行。 因此,该模式应与所选列名上的filter一起使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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