R read.csv how to ignore carriage return?

断了今生、忘了曾经 提交于 2020-02-21 14:51:53

问题


I need to read a text file (tab-separated) that has some carriage returns inside some fields.

If I use read.table, it gives me an error:

line 6257 did not have 20 elements

If I use read.csv, it doesn't give an error, but creates a new line in that place, putting the next fields in the first fields of the new line.

How can I avoid this? I can't alter the file itself (the script is to be run elsewhere). Also the broken strings don't have quotation marks (no strings in the file have). One option would be to read the carriage return as a single space, or as \n, but how?


回答1:


Use read.table instead of read.csv and set allowEscapes to TRUE.

read.table("your/path",sep=",",allowEscapes=TRUE)

I tested w/ the following:

  1. wrote a csv file in excel

contents of csv file:

1,df,3,"4 
"
df,"df
",3,a

result:

  V1   V2 V3   V4
1  1   df  3 4 \n
2 df df\n  3    a


来源:https://stackoverflow.com/questions/30781666/r-read-csv-how-to-ignore-carriage-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!