R 行结束符号不清楚的表格中的读取问题
我目前正在尝试读取.txt文件 我在这里进行了研究,发现——然而,这并没有解决我的问题 这些数据是美国联邦选举委员会在 在检查了.txt之后,我意识到数据的结构很奇怪。特别是,任何一行的结尾都和下一行的第一个单元格完全不分开(不是用“|”分隔,也不是用空格分隔) 奇怪的是,通过Excel和Access导入似乎工作得很好。但是,R导入不起作用 为了避免扫描中出现R 行结束符号不清楚的表格中的读取问题,r,R,我目前正在尝试读取.txt文件 我在这里进行了研究,发现——然而,这并没有解决我的问题 这些数据是美国联邦选举委员会在 在检查了.txt之后,我意识到数据的结构很奇怪。特别是,任何一行的结尾都和下一行的第一个单元格完全不分开(不是用“|”分隔,也不是用空格分隔) 奇怪的是,通过Excel和Access导入似乎工作得很好。但是,R导入不起作用 为了避免扫描中出现错误(file=file,what=what,sep=sep,quote=quote,dec=dec,:第90行没有27个元素错误,我使用
错误(file=file,what=what,sep=sep,quote=quote,dec=dec,:第90行没有27个元素错误,我使用以下命令:
webk14 <- read.table(header = FALSE, fill = TRUE, colClasses = "character", sep = "|", file = "webk14.txt", stringsAsFactors = FALSE, dec = ".", col.names = c("cmte_id", "cmte_nm", "cmte_tp", "cmte_dsgn", "cmte_filing_freq", "ttl_receipts", "trans_from_aff", "indv_contrib", "other_pol_cmte_contrib", "cand_contrib", "cand_loans", "ttl_loans_received", "ttl_disb", "tranf_to_aff", "indv_refunds", "other_pol_cmte_refunds", "cand_loan_repay", "loan_repay", "coh_bop", "coh_cop", "debts_owed_by", "nonfed_trans_received", "contrib_to_other_cmte", "ind_exp", "pty_coord_exp", "nonfed_share_exp","cvg_end_dt"))
webk14它可能与变量名中的符号有关,因此请使用comment.char=”“
来解释这些符号,这将为您提供:
webk14 <- read.table(header = FALSE, fill = TRUE, colClasses = "character", comment.char="",sep = "|",file = "webk14.txt", stringsAsFactors = FALSE, dec = ".", col.names = c("cmte_id", "cmte_nm", "cmte_tp", "cmte_dsgn", "cmte_filing_freq", "ttl_receipts", "trans_from_aff", "indv_contrib", "other_pol_cmte_contrib", "cand_contrib", "cand_loans", "ttl_loans_received", "ttl_disb", "tranf_to_aff", "indv_refunds", "other_pol_cmte_refunds", "cand_loan_repay", "loan_repay", "coh_bop", "coh_cop", "debts_owed_by", "nonfed_trans_received", "contrib_to_other_cmte", "ind_exp", "pty_coord_exp", "nonfed_share_exp","cvg_end_dt"))
webk14只需使用read.delim
instrade ofread.table
。这非常有效-谢谢!