在 R 中解析序列化数据
Parse serialized data in R
我正在尝试将大型数据集读入 R 中的数据框中。数据如下所示(但有很多很多列):
\xk:1520890\xdt:2015031901053801\xty:M\nty:ID\qty:0.00\qoh:0.00\qt:0.00\qp:0.00\wqty:\qre:0
\xk:1520897\xdt:2015031901064000\xty:M\nty:IA\qty:0.00\qoh:0.00\qt:0.00\qp:0.00\wqty:\qre:0
\xk:1520900\xdt:2015031901071000\xty:M\nty:ID\qty:0.00\qoh:0.00\qt:0.00\qp:0.00\wqty:\qre:0
当然有办法做到这一点,但我不知道去哪里找。
在类似 UNIX 的系统中(但在 R 中)你可以这样做:
system("tr -d '\' < test.txt")
#-----output-------
xk:1520890xdt:2015031901053801xty:Mnty:IDqty:0.00qoh:0.00qt:0.00qp:0.00wqty:qre:0
xk:1520897xdt:2015031901064000xty:Mnty:IAqty:0.00qoh:0.00qt:0.00qp:0.00wqty:qre:0
xk:1520900xdt:2015031901071000xty:Mnty:IDqty:0.00qoh:0.00qt:0.00qp:0.00wqty:qre:0
我不确定你是否给出了完整的描述,但在我的 Mac 上,这成功实现了我认为所需的(假设文件在你的工作目录中命名为 test.txt":
inp <- system("tr -d '\' < test.txt", intern=TRUE)
# sed might work too, but I couldn't get the correct sub-pattern.
gsub("[^0-9.]+", " ", inp)
#---------------
[1] " 1520890 2015031901053801 0.00 0.00 0.00 0.00 0"
[2] " 1520897 2015031901064000 0.00 0.00 0.00 0.00 0"
[3] " 1520900 2015031901071000 0.00 0.00 0.00 0.00 0"
如果您有 Windows 台机器,您可能需要使用 shell
。
我正在尝试将大型数据集读入 R 中的数据框中。数据如下所示(但有很多很多列):
\xk:1520890\xdt:2015031901053801\xty:M\nty:ID\qty:0.00\qoh:0.00\qt:0.00\qp:0.00\wqty:\qre:0
\xk:1520897\xdt:2015031901064000\xty:M\nty:IA\qty:0.00\qoh:0.00\qt:0.00\qp:0.00\wqty:\qre:0
\xk:1520900\xdt:2015031901071000\xty:M\nty:ID\qty:0.00\qoh:0.00\qt:0.00\qp:0.00\wqty:\qre:0
当然有办法做到这一点,但我不知道去哪里找。
在类似 UNIX 的系统中(但在 R 中)你可以这样做:
system("tr -d '\' < test.txt")
#-----output-------
xk:1520890xdt:2015031901053801xty:Mnty:IDqty:0.00qoh:0.00qt:0.00qp:0.00wqty:qre:0
xk:1520897xdt:2015031901064000xty:Mnty:IAqty:0.00qoh:0.00qt:0.00qp:0.00wqty:qre:0
xk:1520900xdt:2015031901071000xty:Mnty:IDqty:0.00qoh:0.00qt:0.00qp:0.00wqty:qre:0
我不确定你是否给出了完整的描述,但在我的 Mac 上,这成功实现了我认为所需的(假设文件在你的工作目录中命名为 test.txt":
inp <- system("tr -d '\' < test.txt", intern=TRUE)
# sed might work too, but I couldn't get the correct sub-pattern.
gsub("[^0-9.]+", " ", inp)
#---------------
[1] " 1520890 2015031901053801 0.00 0.00 0.00 0.00 0"
[2] " 1520897 2015031901064000 0.00 0.00 0.00 0.00 0"
[3] " 1520900 2015031901071000 0.00 0.00 0.00 0.00 0"
如果您有 Windows 台机器,您可能需要使用 shell
。