R:仅在文本上使用双引号导入CSV

时间:2022-09-17 18:55:31

I am trying to import a CSV where the text columns are in double quotes but the numeric columns are not. I have 95 columns in total so I cannot manually modify the columns. (Note: There are commas in the text that I cannot remove)

我正在尝试导入CSV,其中文本列是双引号但数字列不是。我总共有95列,所以我无法手动修改列。 (注意:文中有逗号我无法删除)

Below is a sample data set to illustrate my problem.

下面是一个示例数据集来说明我的问题。

CSV File:

CSV文件:

"Column 1","Column 2","Column 3","Column 4","Column 5"

2,"A,A","B,Z","C,C",44

3,"A,X","B,B","C,C",121

Desired Output:

期望的输出:

Column 1    Column 2  Column3   Column4 Column5
   2          A,A      B,Z        C,C       44
   3          A,X      B,B        C,C      121

I tried:

我试过了:

       test=read.csv('test.csv', header=TRUE,sep =",")

2 个解决方案

#1


1  

tf <- tempfile()
csv <- '"Column 1","Column 2","Column 3","Column 4","Column 5"\n\n2,"A,A","B,Z","C,C",44\n\n3,"A,X","B,B","C,C",121'
writeLines( csv , tf )
x <- read.csv( tf )

      Column.1 Column.2 Column.3 Column.4 Column.5
1        2      A,A      B,Z      C,C       44
2        3      A,X      B,B      C,C      121

#2


0  

Remove blank lines as read.table can't accept them

删除空白行,因为read.table无法接受它们

readLines(textConnection(txt)) -> rl  # readLines('csvfile.csv') for you
rl[1:length(rl) %% 2 == 1] -> rl2
read.table(text = rl2, sep = ',', h = TRUE)

Output:

输出:

  Column.1 Column.2 Column.3 Column.4 Column.5
1        2      A,A      B,Z      C,C       44
2        3      A,X      B,B      C,C      121

Data:

数据:

txt <- '"Column 1","Column 2","Column 3","Column 4","Column 5"

2,"A,A","B,Z","C,C",44

3,"A,X","B,B","C,C",121'

#1


1  

tf <- tempfile()
csv <- '"Column 1","Column 2","Column 3","Column 4","Column 5"\n\n2,"A,A","B,Z","C,C",44\n\n3,"A,X","B,B","C,C",121'
writeLines( csv , tf )
x <- read.csv( tf )

      Column.1 Column.2 Column.3 Column.4 Column.5
1        2      A,A      B,Z      C,C       44
2        3      A,X      B,B      C,C      121

#2


0  

Remove blank lines as read.table can't accept them

删除空白行,因为read.table无法接受它们

readLines(textConnection(txt)) -> rl  # readLines('csvfile.csv') for you
rl[1:length(rl) %% 2 == 1] -> rl2
read.table(text = rl2, sep = ',', h = TRUE)

Output:

输出:

  Column.1 Column.2 Column.3 Column.4 Column.5
1        2      A,A      B,Z      C,C       44
2        3      A,X      B,B      C,C      121

Data:

数据:

txt <- '"Column 1","Column 2","Column 3","Column 4","Column 5"

2,"A,A","B,Z","C,C",44

3,"A,X","B,B","C,C",121'