将长字符串转换为data.frame

时间:2015-11-29 18:37:12

标签: r csv dataframe read.table

这是一个菜鸟问题,但我对此感到疯狂。我有一个名为bars.list的字符向量,我从FTP Sever下载。矢量看起来像这样:

"\"\",\"times\",\"open\",\"high\",\"low\",\"close\",\"numEvents\",\"volume\"\r\n\"1\",2015-05-18 06:50:00,23.98,23.98,23.5,23.77,421,0\r\n\"2\",2015-05-18 07:50:00,23.77,23.9,23.34,23.6,720,0\r\n\"3\",2015-05-18 08:50:00,23.6,23.6,23.32,23.42,720,0\r\n\"4\",2015-05-18 09:50:00,23.44,23.91,23.43,23.66,720,0\r\n\"5\",2015-05-18 10:50:00,23.67,24.06,23.59,24.02,720,0\r\n\"6\",2015-05-18 11:50:00,24.02,24.04,23.32,23.33,720,0\r\n\"7\",2015-05-18 12:50:00,23.33,23.42,22.74,22.81,720,0\r\n\"8\",2015-05-18 13:50:00,22.79,22.92,22.49,22.69,720,0\r\n\"9\",2015-05-18 14:50:00,22.69,22.7,22.14,22.14,481,0\r\n\"10\",2015-05-19 06:50:00,21.09,21.49,20.82,21.47,421,0\r\n\"11\",2015-05-19 07:50:00,21.48,21.68,21.46,21.51,720,0\r\n\"12\",2015-05-19 08:50:00,21.51,21.93,21.45,21.92,720,0\r\n\"13\",2015-05-19 09:50:00,21.92,21.92,21.55,21.55,720,0\r\n\"

我需要将此向量转换为可用格式,但

> read.table(bars.list, header = TRUE, sep = ",", quote = "", dec = ".")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file '"","times","open","high","low","close","numEvents","volume"
"1",2015-05-18 06:50:00,23.98,23.98,23.5,23.77,421,0
"2",2015-05-18 07:50:00,23.77,23.9,23.34,23.6,720,0
"3",2015-05-18 08:50:00,23.6,23.6,23.32,23.42,720,0
"4",2015-05-18 09:50:00,23.44,23.91,23.43,23.66,720,0

我不清楚为什么R告诉我某些Connection无法打开,因为该对象已作为参数粘贴到该函数中。输出R向我显示警告标志已经非常接近我需要的......

1 个答案:

答案 0 :(得分:4)

以下是两个选项。第一个提供了对当前代码的修复,第二个提供了一个更简单,更有效的替代方案。

选项1: read.table()中的第一个参数是file。您正在从向量中读取而不是从文件中读取,因此您需要使用text参数,text = bars.list

此外,我们可以先使用gsub()删除所有引号然后再使用 read.csv()代替read.table(),因为header = TRUEsep = ","是默认值。

read.csv(text = gsub("\"", "", bars.list), row.names = 1)
#                  times  open  high   low close numEvents volume
# 1  2015-05-18 06:50:00 23.98 23.98 23.50 23.77       421      0
# 2  2015-05-18 07:50:00 23.77 23.90 23.34 23.60       720      0
# 3  2015-05-18 08:50:00 23.60 23.60 23.32 23.42       720      0
# 4  2015-05-18 09:50:00 23.44 23.91 23.43 23.66       720      0
# 5  2015-05-18 10:50:00 23.67 24.06 23.59 24.02       720      0
# 6  2015-05-18 11:50:00 24.02 24.04 23.32 23.33       720      0
# 7  2015-05-18 12:50:00 23.33 23.42 22.74 22.81       720      0
# 8  2015-05-18 13:50:00 22.79 22.92 22.49 22.69       720      0
# 9  2015-05-18 14:50:00 22.69 22.70 22.14 22.14       481      0
# 10 2015-05-19 06:50:00 21.09 21.49 20.82 21.47       421      0
# 11 2015-05-19 07:50:00 21.48 21.68 21.46 21.51       720      0
# 12 2015-05-19 08:50:00 21.51 21.93 21.45 21.92       720      0
# 13 2015-05-19 09:50:00 21.92 21.92 21.55 21.55       720      0

对我而言,这比使用quote中的read.csv()参数更有效。

data.table 包中的选项2 fread()也可以正常运行。它更快,代码更清晰。无需使用gsub()。我们可以直接放入bars.list并删除第一列。

data.table::fread(bars.list, drop = 1)

现在,由于最终\"引用,您将收到此方法的警告。您可以使用它,也可以通过删除最后一个引号来获得无警告结果。

data.table::fread(sub("\"$", "", bars.list), drop = 1)

数据:

bars.list <- "\"\",\"times\",\"open\",\"high\",\"low\",\"close\",\"numEvents\",\"volume\"\r\n\"1\",2015-05-18 06:50:00,23.98,23.98,23.5,23.77,421,0\r\n\"2\",2015-05-18 07:50:00,23.77,23.9,23.34,23.6,720,0\r\n\"3\",2015-05-18 08:50:00,23.6,23.6,23.32,23.42,720,0\r\n\"4\",2015-05-18 09:50:00,23.44,23.91,23.43,23.66,720,0\r\n\"5\",2015-05-18 10:50:00,23.67,24.06,23.59,24.02,720,0\r\n\"6\",2015-05-18 11:50:00,24.02,24.04,23.32,23.33,720,0\r\n\"7\",2015-05-18 12:50:00,23.33,23.42,22.74,22.81,720,0\r\n\"8\",2015-05-18 13:50:00,22.79,22.92,22.49,22.69,720,0\r\n\"9\",2015-05-18 14:50:00,22.69,22.7,22.14,22.14,481,0\r\n\"10\",2015-05-19 06:50:00,21.09,21.49,20.82,21.47,421,0\r\n\"11\",2015-05-19 07:50:00,21.48,21.68,21.46,21.51,720,0\r\n\"12\",2015-05-19 08:50:00,21.51,21.93,21.45,21.92,720,0\r\n\"13\",2015-05-19 09:50:00,21.92,21.92,21.55,21.55,720,0\r\n\""