Suppose this dataset:
date exdate flag V1 V2
1996-01-04 1996-01-20 P 400000 -0.001181
1996-01-04 1996-01-20 C 400000 -0.004897
.............
1996-01-04 1996-01-20 P 530000 -0.005147
.............
1996-01-04 1996-01-20 P 535000 -0.005423
.............
1996-01-04 1996-01-20 C 545000 -0.007922
1996-01-04 1996-01-20 P 545000 -0.008389
I have ordered the data in the desired format, and I want to only extract the rows that are in the same group of date
, exdate
, and V1
, but remove the groups with a single observation. Note that there are groups that contain only one observation, and not both C
and P
(in the flag variable) as in the first and last group.
It is possible through a package in R like dplyr::filter
?
I have tried this:
data %>% group_by(date,exdate,V1) %>% filter(V1[flag=="P"]==V1[flag=="C"])
with a returning error:
Error in filter_impl(.data, dots) : incorrect length (0), expecting: 1
The goal is to get a dataset that is like this:
date exdate flag V1 V2
1996-01-04 1996-01-20 P 400000 -0.001181
1996-01-04 1996-01-20 C 400000 -0.004897
.............
1996-01-04 1996-01-20 C 545000 -0.007922
1996-01-04 1996-01-20 P 545000 -0.008389