我正在尝试在 R 中创建有关数据验证的报告;我已经使用了 validate 包来生成数据的一般摘要,但我需要了解我们的验证检查失败的细节。
我想要结束的是一个 id 数据框、未通过测试的列以及未通过测试的值。但是,并非所有列都是强制性的,因此我需要能够在不知道该列是否存在的情况下检查数据是否通过。
对于其他具有强制性数据的数据框,我将其转换为 True/False 是否通过测试。例如:
library(dplyr)
library(validate)
library(tidyr)
test_df = data.frame(id = 1:10,
a = 11:20,
b = c(21:25,36,27:30),
c = c(41,52,43:50))
text_check = test_df %>% transmute(
a = a>21,
b = b > 31,
c = c> 51
)
value_fails<-data.frame(id = test_df$id, text_check[,-1][colSums(text_check[,-1]) > 0])
value_failures_gath = gather(value_fails, column, changed, -id) %>% filter(changed == TRUE)
value_failures_gath$Value = apply(value_failures_gath, c(1), function(x)
test_df[test_df$id == x[['id']], grep(x[['column']], colnames(test_df))])
value_failures_gath<-value_failures_gath %>% arrange(id, column)
value_failures_gath$changed<-NULL
colnames(value_failures_gath)<-c('ID','Field','Value')
> value_failures_gath
ID Field Value
1 2 c 52
2 6 b 36
我有一个带有我想要创建的检查的数据框,其样式为:
second_data_check = data.frame(a = 'a>21',
b = 'b > 31',
c = 'c> 51',
d = 'd> 61')
我不能按原样运行这些,因为我们没有要检查的 D 列,但是通过此验证运行的其他数据框可能具有 D 列但没有 B 列。我可以过滤此数据框以仅包含我们拥有的列的测试,但是有没有办法将此数据框中的测试应用为检查?有一个更好的方法吗?
非常感谢你的帮忙!