Простой способ сделать это - изменить форму df
с широкого на длинный формат, чтобы упростить фильтрацию и объединение данных, чтобы получить нужные вам строки.
library(tidyr)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df = data.frame(x = c("P1","P2","P3","P4","P5"),
f_1 = c("NA","1","NA","NA","NA"),
f_2= c("NA","1","NA","NA","NA"),
f_3= c("1","7","NA","NA","NA"),
f_4= c("NA","NA","5","NA","NA"),
f_5= c("NA","NA","2","NA","NA"),
stringsAsFactors = FALSE)
df2 = data.frame(x = c("D1","D2","D3","D4","D5"),
f=c("f_1","f_20","f_30","f_4","f_15"),
stringsAsFactors = FALSE)
# reshape df from wide to long format
longdf <- df %>% gather(f, fvalue, -x)
# to the data wrangling to get your required output
df3 <- df2 %>%
left_join(longdf, by = c("f" = "f")) %>%
rename(x = x.x, fx = f, f = x.y, New = fvalue) %>%
arrange(x, New) %>%
group_by(x) %>%
slice(1) %>%
ungroup() %>%
select(-fx)
df3
#> # A tibble: 5 x 3
#> x f New
#> <chr> <chr> <chr>
#> 1 D1 P2 1
#> 2 D2 <NA> <NA>
#> 3 D3 <NA> <NA>
#> 4 D4 P3 5
#> 5 D5 <NA> <NA>
Создано в 2020-02-04 пакетом представ (v0.3.0)