gpt4 book ai didi

r - 如果变量观察出现在所有组中,则在 R 中过滤 tibble

转载 作者:行者123 更新时间:2023-12-04 02:35:51 26 4
gpt4 key购买 nike

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}" )
)

数据在此小标题中给出(上面的代码):

# 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}

我想排除 1 行,因为日期 2000-01-01 不适用于 tibble 中的所有 stock

最佳答案

一个选项可能是:

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}

关于r - 如果变量观察出现在所有组中,则在 R 中过滤 tibble,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61913729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com