How to read a .csv file containing apostrophes into R?

▼魔方 西西 提交于 2019-12-21 03:09:10

问题


I am having difficulty getting R to read a .txt or .csv file that contains apostrophes.

Some of my columns contain descriptive text, such as "Attends to customers' needs" or "Sheriff's deputy". My file opens correctly in Excel (that is, all the data appear in the correct cells; there are 3 columns and about 8000 rows, and there is no missing data). But when I ask R to read the file, this is what happens:

data <-read.table("datafile.csv", sep=",", header=TRUE)
  Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 520 did not have 3 elements

(Line 520 is the first line that contains an apostrophe.)

If I go into the .txt or .csv file and manually remove all the apostrophes, then R reads the file correctly. However, I'd rather keep the apostrophes if I can.

I am new to R and would be grateful for any help.


回答1:


By default, read.table sees single and double quotes as quoting characters. You need to add quote="\"" to your read.table call. Or, you could just use read.csv, which only sees double quotes as quoting characters by default.




回答2:


Thoroughly studying the options in ?read.table will pay off in the long run. The default values for quoting characters is quote = "\"'", which is really only two characters after R parses that expression, single-quote and double-quote. You can remove them both from consideration using quotes=NA. It's sometimes necessary to also remove the 'comment.char' defaulting to "#", and it may be helpful to change 'as.is' to TRUE to prevent strings from getting converted to factors.




回答3:


Setting the parameter quote="\\" in read.table should do the trick.



来源:https://stackoverflow.com/questions/9620155/how-to-read-a-csv-file-containing-apostrophes-into-r

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