【发布时间】:2018-06-18 10:50:39
【问题描述】:
如果我有两个表 (File1) 和 (File2)
> dput(File1)
structure(list(Column.1 = structure(1:3, .Label = c("Row 1",
"Row 2", "Row 3"), class = "factor"), Column.2 = c(NA, NA, NA
), Column.3 = c(NA, NA, NA), colNames = c(TRUE, TRUE, TRUE)), class = "data.frame", row.names = c(NA,
-3L))
> dput(File2)
structure(list(Column.1 = structure(1:3, .Label = c("Row 1",
"Row 2", "Row 3"), class = "factor"), Column.2 = c(1, 2, 34),
Column.3 = c(NA, NA, NA), colNames = c(TRUE, TRUE, TRUE)), class = "data.frame", row.names = c(NA,
-3L))
我想确认文件 1 和文件 2 之间的列名、列类型以及行数和列数,如果它们都相同则返回 TRUE,否则返回 FALSE,我该如何添加这段代码是我写的?
我尝试了Compare column types between two data frames 中的一些答案,但我只是在寻找正确或错误的答案。这是我当前的代码。
check_file <- function(File1 , File2) {
if (!nrow(File1) == nrow(File2)) {
print("Non matching number of rows")
return(FALSE)
} else if (!ncol(File1) == ncol(File2)) {
print("non matching number of columns")
return(FALSE)
} else if (length(grep("FALSE", names(File1) == names(File2)))>0){
print("Non matching names of columns")
return(FALSE)
}else if (!class(File1)==class(File2)){
print("Non matching column types")
}
return(TRUE)
}
check <- check_file(File1, File2)
if (check) {
return(TRUE)
} else{
return(FALSE)
}
我认为剩下的就是类型。例如,在 dput File 2 Column 2 中有数字,而 File 1 有 NA。它们不必是相同的数字,但它需要返回 false,因为它是 NA。如果文件 1 有 3,2,564,它应该返回 TRUE。
【问题讨论】:
标签: r validation difference