在R中过滤和合并数据

时间:2019-09-16 06:38:40

标签: r dplyr

我有两组数据,我想将结果框架与得分框架(但仅在等级等于2的情况下)left_join。

我认为有一种比过滤分数数据帧,将数据连接并将该帧添加到排名1和3的结果更简单的方法。

有人可以建议这样做的更快方法吗?

我的输入是:

scores = structure(list(Name = c("Frank", "John", "Mary", "Sue", "Tony", 
"Frank", "John", "Mary", "Sue", "Tony", "Frank", "John", "Mary", 
"Sue", "Tony"), Rank = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L), Score = c(51L, 55L, 57L, 75L, 52L, 40L, 
33L, 32L, 44L, 31L, 15L, 12L, 15L, 10L, 17L)), class = "data.frame", row.names = c(NA, 
-15L))

results = structure(list(Name = c("Frank", "John", "Mary", "Sue", "Tony"
), Result = c(250L, 210L, 280L, 260L, 100L)), class = "data.frame", row.names = c(NA, 
-5L))

我正试图得到这个:

output = structure(list(Name = c("Frank", "John", "Mary", "Sue", "Tony", 
"Frank", "John", "Mary", "Sue", "Tony", "Frank", "John", "Mary", 
"Sue", "Tony"), Rank = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L), Score = c(64L, 69L, 50L, 57L, 72L, 40L, 
46L, 47L, 41L, 38L, 29L, 19L, 25L, 11L, 19L), Results = c(NA, 
NA, NA, NA, NA, 250L, 210L, 280L, 260L, 100L, NA, NA, NA, NA, 
NA)), class = "data.frame", row.names = c(NA, -15L))

谢谢。

1 个答案:

答案 0 :(得分:0)

您似乎所需要做的就是在Rank上进行合并和过滤,即

library(dplyr)

scores %>% 
 left_join(results, by = 'Name') %>% 
 mutate(Result = replace(Result, Rank != 2, NA))

给出,

    Name Rank Score Result
1  Frank    1    51     NA
2   John    1    55     NA
3   Mary    1    57     NA
4    Sue    1    75     NA
5   Tony    1    52     NA
6  Frank    2    40    250
7   John    2    33    210
8   Mary    2    32    280
9    Sue    2    44    260
10  Tony    2    31    100
11 Frank    3    15     NA
12  John    3    12     NA
13  Mary    3    15     NA
14   Sue    3    10     NA
15  Tony    3    17     NA