【发布时间】:2018-03-05 16:19:20
【问题描述】:
我正在尝试在我的 R 脚本中插入一个检查步骤,以确定我正在读取的 CSV 表的结构是否符合预期。 查看详细信息: table.csv 具有以下列名: [1] "A","B","C","D"
这个文件是由其他人生成的,因此我想在我的脚本开始时确保列名和列的数量/顺序没有改变。
我尝试执行以下操作:
#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...
提前感谢您的帮助!
【问题讨论】:
-
我想你想要
if(any(Current_Columns != Correct_Columns))。我假设您之前遇到了一些错误?包含有关您的解决方案为何不起作用的信息会有所帮助。
标签: r