r - Keeping only common rows in all groups
问题描述
I have a dataset that contains ten groups. Some observations (rows) are missing from some groups. I want to keep only those observations that are common in each group. I try to make a minimal example. In that example, I have made three groups. In the first group, one observation is missing. Therefore output should me in each group there will be two observations.
library(tidyverse)
## data_set
test_df<-data.frame(groups=c(1,1,1,2,2,2,3,3,3),date=as.Date(c("2000-01-01","2000-01-02","2000-01-03","2000-01-01","2000-01-02","2000-01-03","2000-01-01","2000-01-02","2000-01-03")),data=c(1,2,NA,3,4,5,6,7,8))
## required_output
## keeping data only with common dates
test_df_new<-test_df[c(1,2,4,5,7,8),]
## groups
test_df_new<-test_df%>%
group_by()%>%
解决方案
首先,我在数据列中找到了带有 NA 的日期:
test_df$date[is.na(test_df$data)]
然后我通过 dplyr 过滤:
test_df %>% filter(date != test_df$date[is.na(test_df$data)])
推荐阅读
- python - 在 Python 中无需替换的 Prospensity-score 匹配
- database - 带有颤振的 sqflite 创建表问题
- javascript - 使用 jQuery 中的切换按钮隐藏某些输入并显示其他输入
- mongodb - MongoDB 无法轮换日志,使已删除的文件保持打开状态
- javascript - Webpack:未知属性“查询”?
- azure-api-management - 未在 Azure API 管理中设置 CORS 和自定义标头
- terminal - 如何在超级终端中将主题重置为默认值
- embedded-linux - 访问树莓派 4 系统定时器
- python - 如何在 python 中使用 *args 减去函数中的所有给定数字?
- python-3.x - 在 python 中,是否可以使用 to_csv() 方法更改 csv 文件的第一行?