使用双引号读取R中的csv文件 [英] Read csv file in R with double quotes
本文介绍了使用双引号读取R中的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个csv文件,如下所示:
Suppose I have a csv file looks like this:
Type,ID,NAME,CONTENT,RESPONSE,GRADE,SOURCE
A,3,"","I have comma, ha!",I have open double quotes",A,""
所需输出应为:
df <- data.frame(Type='A',ID=3, NAME=NA, CONTENT='I have comma, ha!',
RESPONSE='I have open double quotes\"', GRADE=A, SOURCE=NA)
df
Type ID NAME CONTENT RESPONSE GRADE SOURCE
1 A 3 NA I have comma, ha! I have open double quotes" A NA
我试图使用 read.csv
,因为数据提供程序使用引号在字符串中转义逗号,但是它们忘记在字符串中转义双引号,不使用逗号,因此无论我是否禁用 read中的引号。 csv
我不会得到所需的输出。
I tried to use read.csv
, since the data provider uses quote to escape comma in the string, but they forgot to escape double quotes in string with no comma, so no matter whether I disable quote in read.csv
I won't get desired output.
我如何在R?
How can I do this in R? Other package solutions are also welcome.
推荐答案
fread
从 data.table
处理这个很好:
library(data.table)
fread('Type,ID,NAME,CONTENT,RESPONSE,GRADE,SOURCE
A,3,"","I have comma, ha!",I have open double quotes",A,""')
# Type ID NAME CONTENT RESPONSE GRADE SOURCE
#1: A 3 I have comma, ha! I have open double quotes" A
这篇关于使用双引号读取R中的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文