如何读取包含撇号到R的.csv文件? [英] How to read a .csv file containing apostrophes into R?

查看:180
本文介绍了如何读取包含撇号到R的.csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让R读取包含撇号的.txt或.csv文件。



我的某些列包含描述性文字,例如关注客户需求或Sheriff's deputy。我的文件在Excel中正确打开(即所有数据显示在正确的单元格中;有3列和约8000行,并且没有丢失数据)。但是当我要求R读取文件时,会发生以下情况:

  data <-read.table(datafile。 csv,sep =,,header = TRUE)
扫描错误(文件,什么,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文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆