我希望我已经正确设置了问题的格式,因为这是我第一次发帖,对R来说还很陌生。
下面是我目前正在使用的一些运动员运动数据的小样本。
```
Player Period Dist Date Type
122 Player_2 Session 4245.9002 31/7/18 Main Training
123 Player_1 Session 4868.2153 2/8/18 Main Training
124 Player_2 Session 4515.1996 2/8/18 Main Training
125 Player_2 Session 3215.8634 7/8/18 Main Training
126 Player_2 Modified 551.8737 7/8/18 Main Training
127 Player_2 Session 4264.7384 9/8/18 Main Training
128 Player_1 Session 4038.1687 16/8/18 Main Training
129 Player_2 Session 4751.6978 16/8/18 Main Training
130 Player_1 RTP 4038.1687 16/8/18 Main Training
131 Player_2 Modified 229.6872 16/8/18 Main Training
132 Player_2 Modified 342.2797 16/8/18 Main Training
133 Player_1 Session 3573.4509 23/8/18 Main Training
134 Player_2 Session 3717.3467 23/8/18 Main Training
reprex()
#> Error: <text>:1:16: unexpected symbol
#> 1: Player Period
#> ^
```
我想使用dplyr
根据多个条件删除数据框的行。具体来说,我想删除包含Session
的行,其中有Modified
或RTP
共享相同的Date
。例如,当Player_2
完成对Modified
的{{1}}培训时,我希望删除该日期的7/8/2018
数据。
Session
与```
Player Period Dist Date Type
125 Player_2 Session 3215.8634 7/8/18 Main Training
126 Player_2 Modified 551.8737 7/8/18 Main Training
#> Error: <text>:1:16: unexpected symbol
#> 1: Player Period
#> ^
```
类似,其中16/8/2018
和Player_1
在当天分别完成了Player_2
和Modified
培训。
RTP
我过去曾经使用这样的代码过滤数据。
```
Player Period Dist Date Type
128 Player_1 Session 4038.1687 16/8/18 Main Training
129 Player_2 Session 4751.6978 16/8/18 Main Training
130 Player_1 RTP 4038.1687 16/8/18 Main Training
131 Player_2 Modified 229.6872 16/8/18 Main Training
132 Player_2 Modified 342.2797 16/8/18 Main Training
#> Error: <text>:1:16: unexpected symbol
#> 1: Player Period
#> ^
```
但是,我希望删除包含```
db18 <- db18 %>%
filter(Period %in% c("Session"))
```
或Session
的运动员Modified
数据,因为它不会“污染”我要执行的分析。想知道如果可能的话我该怎么做。
任何帮助将不胜感激。谢谢。
答案 0 :(得分:2)
希望这对您有所帮助。
player <- read.csv("player.csv")
player
player
Id Player Period Dist Date Type
1 122 Player_2 Session 4245.9002 31/07/18 Main Training
2 123 Player_1 Session 4868.2153 02/08/18 Main Training
3 124 Player_2 Session 4515.1996 02/08/18 Main Training
4 125 Player_2 Session 3215.8634 07/08/18 Main Training
5 126 Player_2 Modified 551.8737 07/08/18 Main Training
6 127 Player_2 Session 4264.7384 09/08/18 Main Training
7 128 Player_1 Session 4038.1687 16/08/18 Main Training
8 129 Player_2 Session 4751.6978 16/08/18 Main Training
9 130 Player_1 RTP 4038.1687 16/08/18 Main Training
10 131 Player_2 Modified 229.6872 16/08/18 Main Training
11 132 Player_2 Modified 342.2797 16/08/18 Main Training
12 133 Player_1 Session 3573.4509 23/08/18 Main Training
13 134 Player_2 Session 3717.3467 23/08/18 Main Training
按Player
和Date
列分组。然后,如果特定的Date
具有
Modified(or)RTP
和Session
。
library(dplyr)
removable <- player %>% group_by_(.dots = c("Player", "Date")) %>%
filter( (sum(Period == 'Session') >= 1) & ((sum(Period == 'Modified') != 0) | (sum(Period == 'RTP') != 0 ))) %>%
filter(Period == 'Session')
如果有任何player
与player$Id
匹配,现在从removable$Id
数据框中删除行
player <- player[!(player$Id %in% removable$Id), ]
player
Id Player Period Dist Date Type
1 122 Player_2 Session 4245.9002 31/07/18 Main Training
2 123 Player_1 Session 4868.2153 02/08/18 Main Training
3 124 Player_2 Session 4515.1996 02/08/18 Main Training
5 126 Player_2 Modified 551.8737 07/08/18 Main Training
6 127 Player_2 Session 4264.7384 09/08/18 Main Training
9 130 Player_1 RTP 4038.1687 16/08/18 Main Training
10 131 Player_2 Modified 229.6872 16/08/18 Main Training
11 132 Player_2 Modified 342.2797 16/08/18 Main Training
12 133 Player_1 Session 3573.4509 23/08/18 Main Training
13 134 Player_2 Session 3717.3467 23/08/18 Main Training
答案 1 :(得分:1)
一种方法是先使用group_by()
函数,然后将过滤器应用于组。在下面的代码中,我使用了group_by()
和mutate()
来创建要过滤的新列。可能会有更优雅的解决方案,但这可能会让您入门。
df <- tibble::tribble(
~Player, ~Period, ~Dist, ~Date,
'Player_2', 'Session', 4245.9002, '31/7/18',
'Player_1', 'Session', 4868.2153, '2/8/18',
'Player_2', 'Session', 4515.1996, '2/8/18',
'Player_2', 'Session', 3215.8634, '7/8/18',
'Player_2', 'Modified', 551.8737, '7/8/18',
'Player_2', 'Session', 4264.7384, '9/8/18',
'Player_1', 'Session', 4038.1687, '16/8/18',
'Player_2', 'Session', 4751.6978, '16/8/18',
'Player_1', 'RTP', 4038.1687, '16/8/18',
'Player_2', 'Modified', 229.6872, '16/8/18',
'Player_2', 'Modified', 342.2797, '16/8/18',
'Player_1', 'Session', 3573.4509, '23/8/18',
'Player_2', 'Session', 3717.3467, '23/8/18'
)
df %>%
group_by(Player, Date) %>%
mutate(filter_col = ifelse(all(c('Session','Modified') %in% Period), 'delete', 'keep'),
filter_col = ifelse(all(c('Session','RTP') %in% Period), 'delete', filter_col)) %>%
ungroup() %>%
filter(filter_col == 'keep')