Bernard.b
Bernard.b

Reputation: 23

How to delete rows in a data.frame by a specific condition?

I tried to delete some rows in a data.frame but can't solve do problem. Can anyone help?

This is the data.frame

   subj trial fix type
1     a     1   1    K
2     a     1   2    T
3     a     1   3    T
4     a     2   1    K
5     a     2   2    K
6     a     2   3    T
7     b     1   1    T
8     b     1   2    K
9     b     1   3    K
10    b     2   1    K
11    b     2   2    T
12    b     2   3    T

and I want for each subj, in each trial the row where T appears for the first time. The result should look like this:

   subj trial fix type
2     a     1   2    T
6     a     2   3    T
7     b     1   1    T
11    b     2   2    T

Upvotes: 1

Views: 59

Answers (3)

akrun
akrun

Reputation: 886968

We could use slice with match

library(dplyr)
df %>% 
     group_by(subj, trial) %>%
     slice(match('T', type))
# A tibble: 4 x 4
# Groups:   subj, trial [4]
#  subj  trial   fix type 
#  <chr> <int> <int> <chr>
#1 a         1     2 T    
#2 a         2     3 T    
#3 b         1     1 T    
#4 b         2     2 T    

data

df <- structure(list(subj = c("a", "a", "a", "a", "a", "a", "b", "b", 
"b", "b", "b", "b"), trial = c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 
1L, 2L, 2L, 2L), fix = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L), type = c("K", "T", "T", "K", "K", "T", "T", "K", 
"K", "K", "T", "T")), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))

Upvotes: 1

Darren Tsai
Darren Tsai

Reputation: 35554

You can use filter() + distinct().

library(dplyr)

df %>%
  filter(type == "T") %>%
  distinct(subj, trial, .keep_all = T)

#    subj trial fix type
# 2     a     1   2    T
# 6     a     2   3    T
# 7     b     1   1    T
# 11    b     2   2    T

Data

df <- structure(list(subj = c("a", "a", "a", "a", "a", "a", "b", "b", 
"b", "b", "b", "b"), trial = c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 
1L, 2L, 2L, 2L), fix = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L), type = c("K", "T", "T", "K", "K", "T", "T", "K", 
"K", "K", "T", "T")), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))

Upvotes: 2

Sotos
Sotos

Reputation: 51582

One idea using slice from dplyr,

library(dplyr)

df %>% 
 group_by(subj, trial) %>% 
 slice(which(type == 'T')[1])

# A tibble: 4 x 4
# Groups:   subj, trial [4]
#  subj  trial   fix type 
#  <fct> <int> <int> <fct>
#1 a         1     2 T    
#2 a         2     3 T    
#3 b         1     1 T    
#4 b         2     2 T

Upvotes: 3

Related Questions