Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查表';在R中读取.csv时,s标头与预期一致_R - Fatal编程技术网

检查表';在R中读取.csv时,s标头与预期一致

检查表';在R中读取.csv时,s标头与预期一致,r,R,我试图在我的R脚本中插入一个检查步骤,以确定我正在读取的CSV表的结构是否符合预期。 详情见: table.csv具有以下列名称: [1] “A”、“B”、“C”、“D” 这个文件是由其他人生成的,因此我希望在脚本开始时确保colnames和列的数量/顺序没有改变 我试着做到以下几点: #dataframes to import df_table <- read.csv('table.csv') #define correct structure of file

我试图在我的R脚本中插入一个检查步骤,以确定我正在读取的CSV表的结构是否符合预期。 详情见: table.csv具有以下列名称: [1] “A”、“B”、“C”、“D”

这个文件是由其他人生成的,因此我希望在脚本开始时确保colnames和列的数量/顺序没有改变

我试着做到以下几点:

    #dataframes to import
    df_table <- read.csv('table.csv')

    #define correct structure of file
    Correct_Columns <- c('A','B','C','D')
    #read current structure of table
    Current_Columns <- colnames(df_table)

    #Check whether CSV was correctly imported from Source
    if(Current_Columns != Correct_Columns)

    {
    # if structure has changed, stop the script. 
    stop('Imported CSV has a different structure, please review export from Source.')
    } 
    #if not, continue with the rest of the script...
#要导入的数据帧

df_table使用数据的快速方法。table:

library(data.table)
DT <- fread("table.csv")
Correct_Columns <- c('A','B','C','D')
Current_Columns <- colnames(df_table)

}

使用base R,我建议您查看
all.equal()
idential()
any()

请参见以下示例:

a <- c(1,2)
b <- c(1,2)
c <- c(1,2)
d <- c(1,2)
df <- data.frame(a,b,c,d)

names.df <- colnames(df)
names.check <- c("a","b","c","d")

!all.equal(names.df,names.check)
# [1] FALSE

!identical(names.df,names.check)
# [1] FALSE

any(names.df!=names.check)
# [1] FALSE
您的代码可能会抛出警告,因为
当前\u列=Correct_Columns
将比较向量的所有条目(即,在控制台上单独运行
Current_Columns!=Correct_Columns
将返回具有真/假值的向量)

相反,
all.equal()
idential()
将比较整个向量,同时将它们视为对象


为了完整起见,请注意
all.equal()
idential()
之间的细微差别。在你的例子中,使用哪一个并不重要,但在处理数值向量时,它会变得很重要。有关更多信息,请参阅。

我想您需要
如果(有(当前列!=正确列))
。我想你以前也犯过错误吧?如果你能告诉我为什么你的解决方案不起作用,那会很有帮助的。非常感谢你,它起了很大的作用!现在唯一的问题是,当我从主脚本中调用脚本时,它将不起作用:>##########>source('location/…[TRUNCATED]eval(ei,envir)中的错误):导入的CSV具有不同的结构,请查看从源代码导出。差点忘了,如果我单独运行同一个脚本,它会工作,并且if实例不会触发,因为文件结构实际上是相同的。只有当我从主脚本运行它时,它才会通过if测试…我猜主脚本的代码是nec我的第一个猜测是,在运行主脚本时,您的工作目录是不同的,因此加载了不同的“table.csv”文件。但这只是一个猜测而已。
a <- c(1,2)
b <- c(1,2)
c <- c(1,2)
d <- c(1,2)
df <- data.frame(a,b,c,d)

names.df <- colnames(df)
names.check <- c("a","b","c","d")

!all.equal(names.df,names.check)
# [1] FALSE

!identical(names.df,names.check)
# [1] FALSE

any(names.df!=names.check)
# [1] FALSE
if(!all.equal(Current_Columns,Correct_Columns))
{
# call your stop statement here
}