根据 data.frame 中的唯一 ID 值捕获错误

Catching an error based on unique id values in a data.frame

我想为下面的 data.frame 创建一个 stop(),这样对于每个唯一的 id,如果 pos 是变化的(例如,由 1s、2s 组成等),那么,如果 cont==TRUE 行的 mp 值不相同,我们应该抛出错误。

这在 R 中可行吗?

在下面的玩具示例中,id == "B" 应该抛出错误,因为 pos 是变化的 (1,2,3),而 mp 值(即 1,3 ) 对于 cont==TRUE 不相同的行。

dat <- data.frame(id = rep(c("A","B"),2:3), mp = c(1,5, 2,1,3), cont = c(F,T, F,T,T), pos = c(1,1, 1:3))
#  id mp  cont pos
#1  A  1 FALSE   1
#2  A  5  TRUE   1
#3  B  2 FALSE   1
#4  B  1  TRUE   2
#5  B  3  TRUE   3

# Desired stop() message:
"Error: 'B' has a wrong value."

base R 中,一个选项是 split 数据子集,即 'cont' 为 TRUE 的 'id' 到 list。然后遍历 list 以及 Maplistnames,检查 if unique 行是否超过 1 行,然后调用 stop

lst1 <- split(dat[dat$cont,c("mp", "pos")], dat$id[dat$cont])
Map(function(x, y) if(nrow(unique(x)) > 1)  
     stop(sprintf("'%s' has a wrong value.", y), call. = FALSE), 
        lst1, names(lst1))
#Error: 'B' has a wrong value.

更新示例

lst1 <- split(dat[dat$control, c("mpre", "post")], dat$study.name[dat$control])
Map(function(x, y) if(all(lengths(lapply(x, unique))  > 1))  
      stop(dQuote(sprintf("'%s' has a wrong value.", y), FALSE), call. = FALSE), 
         lst1, names(lst1))
#Error: "'Brown' has a wrong value."