问题描述
我无法让R读取包含撇号的.txt或.csv文件。
我的某些列包含描述性文字,例如关注客户需求或Sheriff's deputy。我的文件在Excel中正确打开(即所有数据显示在正确的单元格中;有3列和约8000行,并且没有丢失数据)。但是当我要求R读取文件时,会发生以下情况:
data 扫描错误(文件,什么,nmax,sep,dec,报价,跳过,nlines,na.strings,:
行520有3个元素
(第520行是包含撇号的第一行。)
如果我进入.txt或.csv文件并手动删除所有撇号,那么R正确读取文件,但如果可以,我宁愿保留撇号。 p>
我是R的新手,非常感谢您的帮助。
默认情况下, read.table
将单引号和双引号看作引号字符,您需要添加 quote =\
到你的 read.table
调用,或者你可以使用 read.csv
默认情况下引号为引号。
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.
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.
这篇关于如何读取包含撇号到R的.csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!