library(tidyverse)
dta <- tibble(
date = c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-02","2000-01-03"),
stock = c("A", "A", "A", "B", "B"),
price = c("price_{A1}","price_{A2}","price_{A3}","price_{B1}","price_{B2}" )
)
The data is given in this tibble (code above):
# A tibble: 5 x 3
date stock price
<chr> <chr> <chr>
1 2000-01-01 A price_{A1}
2 2000-01-02 A price_{A2}
3 2000-01-03 A price_{A3}
4 2000-01-02 B price_{B1}
5 2000-01-03 B price_{B2}
I want to exclude row 1, since date 2000-01-01 is not available for all stocks in the tibble.
One option could be:
library(dplyr)
dta %>%
group_by(date) %>%
filter(n_distinct(stock) != 1)
# A tibble: 4 x 3
# Groups: date [2]
date stock price
<chr> <chr> <chr>
1 2000-01-02 A price_{A2}
2 2000-01-03 A price_{A3}
3 2000-01-02 B price_{B1}
4 2000-01-03 B price_{B2}
Or:
library(tidyverse)
dta <- tibble(
date = c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-02","2000-01-03"),
stock = c("A", "A", "A", "B", "B"),
price = c("price_{A1}","price_{A2}","price_{A3}","price_{B1}","price_{B2}" )
)
dta %>%
filter(date %in% reduce(split(.$date, .$stock), intersect))
# date stock price
<chr> <chr> <chr>
# 1 2000-01-02 A price_{A2}
# 2 2000-01-03 A price_{A3}
# 3 2000-01-02 B price_{B1}
# 4 2000-01-03 B price_{B2}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With