0

I was trying to use a csv file in R in read.transactions() command from arules package.

The csv file when opened in Notepad++ shows extra commas for every non-existing values. So, I'm having to manually delete those extra commas before using the csv in read.transactions(). For example, the actual csv file when opened in Notepad++ looks like:

D115,DX06,Slz,,,,
HC,,,,,,
DX06,,,,,,
DX17,PG,,,,,
DX06,RT,Dty,Dtcr,,

I want it to appear like below while sending it into read.transactions():

D115,DX06,Slz
HC
DX06
DX17,PG
DX06,RT,Dty,Dtcr

Is there any way I can make that change in read.transactions() itself, or any other way? But even before that, we don't get to see those extra commas in R(that output I showed was from Notepad++)..

So how can we even remove them in R when we can't see it?

4

1 回答 1

3

创建一个没有尾随逗号的新文件的简单方法是:

file_lines <- readLines("input.txt")
writeLines(gsub(",+$", "", file_lines),
           "without_commas.txt")

gsub命令中,匹配行尾 ( ) 处的",+$"一个或多个 ( +) 逗号 ( )。,$

由于您使用的是 Notepad++,因此您可以在该程序中进行替换:搜索 > 替换,替换,+$为空,搜索模式=正则表达式。

于 2015-05-08T11:11:53.247 回答