繁体   English   中英

从 R 导出对象,以便将它们导入 Python 脚本

[英]export objects from R, so they can be imported into a Python script

我不太了解 R,但我有一个用 R 编写的脚本。 该脚本的作用是基于一个时间序列,它创建一个如下表所示的表。

我需要导出的几百个表之一

我设法分析了多个时间序列并将所有表创建为 R 中的对象。 我当然可以将这些单独写入 excel 文件,我可以使用 Python 读取该文件,但这不是很方便,因为我有数百个这样的表。

这些表对象具有以下 class:

在此处输入图像描述

我试图将它们放入某种字典中,其中每个表在导入 python 后可以单独调用。 然后我找到了一个可以将对象导出到 RData 的选项。 这很完美,我可以导入 python,但我不知道如何为所有表自动执行此操作。

save(cluster0,cluster1,cluster2, file = "/Users/jerjely/Desktop/burst.RData")

我发现的另一个选择是保存所有内容,如下所示:

save.image(file = "/Users/jerjely/Desktop/bursts.RData")

但是我无法读入 python,因为它有无法识别的对象。 我假设是因为 R 中定义的功能。

总而言之,如果有人能告诉我如何将所有表的列表填写到第一个命令中,我将非常高兴。 或者我的另一个想法是是否可以删除函数和对象,所以我只能导出所需的对象。

任何帮助是极大的赞赏!

非常感谢!

有这个: Loading.RData files into Python和更现代的可能,这个: How to load R's.rdata files into Python?

搜索python 读取 rdata给了我很多结果。

对于简单的结构 json 在其他方面很好。


library(jsonlite)

l <- list(
    cluster1, cluster2, cluster3
)

cat(
    toJSON( l ),
    file="/some/file.json"
)

然后改为阅读。 Python 读取 json 作为早餐。

我注意到这不会保留列名。 (可能也不是行名):

我没有你的数据,所以再次抓起可怜的花朵:

l <- rep(list(head(as.matrix(iris[,1:4]))), 3 )
cat( toJSON( l, pretty=T ) )

[
  [
    [5.1, 3.5, 1.4, 0.2],
    [4.9, 3, 1.4, 0.2],
    [4.7, 3.2, 1.3, 0.2],
    [4.6, 3.1, 1.5, 0.2],
    [5, 3.6, 1.4, 0.2],
    [5.4, 3.9, 1.7, 0.4]
  ],
  [
    [5.1, 3.5, 1.4, 0.2],
    [4.9, 3, 1.4, 0.2],
    [4.7, 3.2, 1.3, 0.2],
    [4.6, 3.1, 1.5, 0.2],
    [5, 3.6, 1.4, 0.2],
    [5.4, 3.9, 1.7, 0.4]
  ],
  [
    [5.1, 3.5, 1.4, 0.2],
    [4.9, 3, 1.4, 0.2],
    [4.7, 3.2, 1.3, 0.2],
    [4.6, 3.1, 1.5, 0.2],
    [5, 3.6, 1.4, 0.2],
    [5.4, 3.9, 1.7, 0.4]
  ]
]

现在,如果您将那些 arrays 转换为 data.frame,您会得到这个结构,在 python 中使用它可能更容易吗?

l <- rep(list(head(as.data.frame(iris[,1:4]))), 3 )
cat( toJSON( l, pretty=T ) )

变成这样:


[
  [
    {
      "Sepal.Length": 5.1,
      "Sepal.Width": 3.5,
      "Petal.Length": 1.4,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 4.9,
      "Sepal.Width": 3,
      "Petal.Length": 1.4,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 4.7,
      "Sepal.Width": 3.2,
      "Petal.Length": 1.3,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 4.6,
      "Sepal.Width": 3.1,
      "Petal.Length": 1.5,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 5,
      "Sepal.Width": 3.6,
      "Petal.Length": 1.4,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 5.4,
      "Sepal.Width": 3.9,
      "Petal.Length": 1.7,
      "Petal.Width": 0.4
    }
  ],
  [
    {
      "Sepal.Length": 5.1,
      "Sepal.Width": 3.5,
      "Petal.Length": 1.4,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 4.9,
      "Sepal.Width": 3,
      "Petal.Length": 1.4,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 4.7,
      "Sepal.Width": 3.2,
      "Petal.Length": 1.3,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 4.6,
      "Sepal.Width": 3.1,
      "Petal.Length": 1.5,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 5,
      "Sepal.Width": 3.6,
      "Petal.Length": 1.4,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 5.4,
      "Sepal.Width": 3.9,
      "Petal.Length": 1.7,
      "Petal.Width": 0.4
    }
  ],
  [
    {
      "Sepal.Length": 5.1,
      "Sepal.Width": 3.5,
      "Petal.Length": 1.4,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 4.9,
      "Sepal.Width": 3,
      "Petal.Length": 1.4,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 4.7,
      "Sepal.Width": 3.2,
      "Petal.Length": 1.3,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 4.6,
      "Sepal.Width": 3.1,
      "Petal.Length": 1.5,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 5,
      "Sepal.Width": 3.6,
      "Petal.Length": 1.4,
      "Petal.Width": 0.2
    },
    {
      "Sepal.Length": 5.4,
      "Sepal.Width": 3.9,
      "Petal.Length": 1.7,
      "Petal.Width": 0.4
    }
  ]
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM