我正在寻找一个带有数据帧列的函数,检查它是否包含来自字符串向量的文本,并在匹配时过滤它(包括部分文本匹配).
例如,采用以下数据框:
animal |count
aardvark |8
cat |2
catfish |6
dog |12
dolphin |3
penguin |38
prairie dog|59
zebra |17
Run Code Online (Sandbox Code Playgroud)
和以下矢量
c("cat", "dog")
Run Code Online (Sandbox Code Playgroud)
我想通过'animal'列,检查值是否完全或部分匹配向量中的一个字符串,并过滤掉那些不匹配的字符串.结果数据框将是:
animal |count
cat |2
catfish |6
dog |12
prairie dog|59
Run Code Online (Sandbox Code Playgroud)
谢谢!
肖恩
我的设置:
我有一些篮球运动员和他们的统计数据。
library(tidyverse)
df <- tibble(
season = c(2010, 2011, 2012, 2013, 2014,
2010, 2011, 2012, 2013, 2014),
player = c("player_a", "player_a", "player_a", "player_a", "player_a",
"league_avg", "league_avg", "league_avg", "league_avg", "league_avg"),
fg_perc = c(.4912, .6083, .3095, .5525, .4289,
.4825, .4836, .4819, .4860, .4848),
points_game = c(20, 18, 15, 19, 18,
12, 12, 13, 11, 12)
)
Run Code Online (Sandbox Code Playgroud)
我已经将某个列 (fg_perc) 显示为 player_a 和 League_avg 的 geom_line()。我还将它包装在一个自定义函数中,因为我将对其他统计数据使用相同的方法。
make_chart <- function(target_column) {
df %>%
ggplot(aes_string("season", target_column, label = target_column)) +
geom_line(aes(color = player), size = …Run Code Online (Sandbox Code Playgroud)