![](/img/trans.png)
[英]How to fill a column from a data frame based on another data frame using dplyr
[英]How to filter a data frame based on another data frame with dplyr
我有以下數據框:
library(tidyverse)
dat <- tribble(
~comp_name, ~model, ~status,
"foo", 0, "A",
"foo", 2, "B",
"foo", 3, "B",
"foo", 11, "C",
"bar", 0, "X",
"bar", 10, "Y",
"bar", 20, "Z"
)
我有另一個數據框,我想用它來過濾掉dat
中的成員。
wanted_model_dat <- tibble::tribble(
~comp_name, ~wanted_model,
"foo", c(2, 11),
"bar", c(0, 1),
)
基本上,對於dat
中的每個comp_name
,我想過濾(保留)值位於 Wanted_model_dat 中相應wanted_model
列中的wanted_model_dat
。 最終想要的結果是這樣的:
comp_name model status
foo 2 B
foo 11 C
bar 0 X
我怎樣才能做到這一點?
你可以試試
library(tidyr)
library(dplyr)
dat %>%
inner_join(unnest(wanted_model_dat , wanted_model),
by = c("comp_name", "model" = 'wanted_model'))
comp_name model status
<chr> <dbl> <chr>
1 foo 2 B
2 foo 11 C
3 bar 0 X
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.