我必须对项目的 .tsv 文件进行一些分析,而且我对 R 相当陌生。在 R 中读取/写入 .tsv 文件时遇到问题。似乎当有引号时就会出现问题( “”)在行中。
原始文件中的一些记录示例如下:
org_id org_name description created at
5762 Artifice Artifice \comes from Latin 4/3/2014 19:42
1045 Access Dar Microsoft "Nasdaq worldwide 7/4/2014 10:34
345 Living Asset Lincoln Park Zoo 11/3/2014 19:42
2356 Adler Planet Mission of black cat 12/2/2014 11:03
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码行读取该文件:
orgs <- read.delim("C:/Users/orgs.tsv", header=TRUE)
Run Code Online (Sandbox Code Playgroud)
重命名列后,我使用以下代码编写文件:
write.table(orgs, file = "C:/Users/orgs_updated.tsv", row.names=FALSE, sep="\t")
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试在另一个程序中读取此文件(orgs_updated.tsv)时,它不喜欢任何列中都有引号。我正在使用下面的代码再次读取该文件:
orgs_updated <- read.delim("C:/Users/orgs_updated.tsv", sep="", header=TRUE, quote="\"")
Run Code Online (Sandbox Code Playgroud)
文件是这样读取的,即读取错误,并添加错误的行。
org_id name description created at
5762 Artifice Artifice \comes from Latin 4/3/2014 19:42
1045 Access Dar Microsoft Nasdaq worldwide
7/4/2014 10:34
345 Living Asset Lincoln Park Zoo 11/3/2014 19:42
2356 Adler Planet Mission of black cat 12/2/2014 11:03
Run Code Online (Sandbox Code Playgroud)
我不确定我做错了什么。我试过:
using the quote=FALSE option in write.table,
not using quote option in the 2nd read.delim
changing sep = "" to sep ="\t"
Run Code Online (Sandbox Code Playgroud)
但无法找出解决方案。
如果有人可以帮忙,我将不胜感激!
尝试使用以下内容加载文件(我在我的计算机上使用逗号分隔而不是制表符创建文件):
orgs <- read.delim("orgs.tsv", header=TRUE, allowEscapes=FALSE, sep=",", quote="", na.strings="", comment.char="")
write.table(orgs, file = "orgs_updated.tsv", row.names=FALSE, sep="\t")
orgs_updated <- read.delim("orgs_updated.tsv", sep="", header=TRUE, quote="\"")
orgs_updated
org_id org_name description created.at
1 5762 Artifice Artifice \\comes from Latin 4/3/2014 19:42
2 1045 Access Dar Microsoft "Nasdaq worldwide 7/4/2014 10:34
3 345 Living Asset Lincoln Park Zoo 11/3/2014 19:42
4 2356 Adler Planet Mission of black cat 12/2/2014 11:03
Run Code Online (Sandbox Code Playgroud)