R -- 在重复序列中查找缺失元素
R -- Finding absent elements in a repeated series
您如何找到应该(可预测地)存在于数据框中但不存在的条目?我可能是微不足道的问题与 this question 类似,但多了一层或两层——我尝试过的每个解决方案都无法解决集合中的重复和不规则问题。
# Over some years, with a few skipped...
yrs <- 1985:2018
yrs <- yrs[-sample(1:length(yrs),5)]
# at a number of plots, replicated over two transects...
pts <- letters[1:10]
lns <- c('N','S')
# a study was done (data fields omitted):
yrsr <- rep(yrs, each= length(lns)*length(pts))
lnsr <- rep(rep(lns, each=length(pts)), times=length(yrs))
ptsr <- rep(rep(pts, times=length(lns)), times=length(yrs))
study <- data.frame(YEAR=yrsr, LINE=lnsr, PLOT=ptsr)
## But for random reasons certain plots got left out.
studym <- study[-sample(1:nrow(study), 23),]
# NB: The number of entries per plot varies:
studym$SPEC <- sample(c(1,1,1,1,1,2,2,2,3), nrow(studym), replace=TRUE)
studyAll <- studym[rep(row.names(studym), studym$SPEC),]
遗漏的图可能是合法的零或数据输入错误或其他;它们需要被追踪并更正或作为 NA 插入。因此,要在原始数据表中找到它们,我需要一个列表... studyAll
中不存在的所有元素...从我的 运行 此处可以是
# 1985 N d
# 1985 N g
# ...
# 2017 S g
但由于它们不存在,我很难弄清楚要请求什么以及从哪里请求。我一直无法找出任何连接来做我想做的事。我得到了一个诱人的总结:
studyAll %>% group_by(YEAR, LINE) %>% count(PLOT) %>% apply(2, table)
但这只是告诉我每个问题有多少,而不是在哪里找到它。
(额外的小问题:有没有一种方法可以直接从 yrs
、pts
和 lns
构造 study
,而无需 [=18 的那三行=]?我认为一定有某种方法可以生成这样的简单层次结构,但找不到。)
在因子设计中查找缺失数据的一种方法是生成
来自 studyAll 的 YEAR、LINE 和 PLOT 的所有组合,然后找到
所有组合与您 studyAll 中记录的观察结果之间的差异 data.frame
通过 anti_join.
library("tidyr")
library("dplyr")
studyMissing <- studyAll %>%
expand(YEAR, LINE, PLOT) %>%
anti_join(studyAll, by = c("YEAR", "LINE", "PLOT"))
# Giving
# A tibble: 23 x 3
# YEAR LINE PLOT
# <int> <fct> <fct>
# 1 1985 N f
# 2 1986 N h
# 3 1986 S g
# 4 1992 N h
# 5 1996 S g
# 6 2001 N e
# 7 2001 N i
# 8 2002 N c
# 9 2002 S g
#10 2003 N h
## ... with 13 more rows
您如何找到应该(可预测地)存在于数据框中但不存在的条目?我可能是微不足道的问题与 this question 类似,但多了一层或两层——我尝试过的每个解决方案都无法解决集合中的重复和不规则问题。
# Over some years, with a few skipped...
yrs <- 1985:2018
yrs <- yrs[-sample(1:length(yrs),5)]
# at a number of plots, replicated over two transects...
pts <- letters[1:10]
lns <- c('N','S')
# a study was done (data fields omitted):
yrsr <- rep(yrs, each= length(lns)*length(pts))
lnsr <- rep(rep(lns, each=length(pts)), times=length(yrs))
ptsr <- rep(rep(pts, times=length(lns)), times=length(yrs))
study <- data.frame(YEAR=yrsr, LINE=lnsr, PLOT=ptsr)
## But for random reasons certain plots got left out.
studym <- study[-sample(1:nrow(study), 23),]
# NB: The number of entries per plot varies:
studym$SPEC <- sample(c(1,1,1,1,1,2,2,2,3), nrow(studym), replace=TRUE)
studyAll <- studym[rep(row.names(studym), studym$SPEC),]
遗漏的图可能是合法的零或数据输入错误或其他;它们需要被追踪并更正或作为 NA 插入。因此,要在原始数据表中找到它们,我需要一个列表... studyAll
中不存在的所有元素...从我的 运行 此处可以是
# 1985 N d
# 1985 N g
# ...
# 2017 S g
但由于它们不存在,我很难弄清楚要请求什么以及从哪里请求。我一直无法找出任何连接来做我想做的事。我得到了一个诱人的总结:
studyAll %>% group_by(YEAR, LINE) %>% count(PLOT) %>% apply(2, table)
但这只是告诉我每个问题有多少,而不是在哪里找到它。
(额外的小问题:有没有一种方法可以直接从 yrs
、pts
和 lns
构造 study
,而无需 [=18 的那三行=]?我认为一定有某种方法可以生成这样的简单层次结构,但找不到。)
在因子设计中查找缺失数据的一种方法是生成 来自 studyAll 的 YEAR、LINE 和 PLOT 的所有组合,然后找到 所有组合与您 studyAll 中记录的观察结果之间的差异 data.frame 通过 anti_join.
library("tidyr")
library("dplyr")
studyMissing <- studyAll %>%
expand(YEAR, LINE, PLOT) %>%
anti_join(studyAll, by = c("YEAR", "LINE", "PLOT"))
# Giving
# A tibble: 23 x 3
# YEAR LINE PLOT
# <int> <fct> <fct>
# 1 1985 N f
# 2 1986 N h
# 3 1986 S g
# 4 1992 N h
# 5 1996 S g
# 6 2001 N e
# 7 2001 N i
# 8 2002 N c
# 9 2002 S g
#10 2003 N h
## ... with 13 more rows