简体   繁体   English

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

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

I don't know too much about R, but I have a script that is written in R.我不太了解 R,但我有一个用 R 编写的脚本。 What the script does, is based on a timeseries it creates a table like the one below.该脚本的作用是基于一个时间序列,它创建一个如下表所示的表。

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

I managed to analyze multiple time series and create all of the tables as objects within R.我设法分析了多个时间序列并将所有表创建为 R 中的对象。 I can of course write these individually to an excel file that I can read in with Python, but that is not very convenient since I have several hundreds of these tables.我当然可以将这些单独写入 excel 文件,我可以使用 Python 读取该文件,但这不是很方便,因为我有数百个这样的表。

These table objects have the following class:这些表对象具有以下 class:

在此处输入图像描述

I was trying to maybe put them in some sort of dictionary, where each of the tables can be called separately after being imported into python.我试图将它们放入某种字典中,其中每个表在导入 python 后可以单独调用。 Then I found an option where I can export objects in to RData.然后我找到了一个可以将对象导出到 RData 的选项。 This works perfect and I can import into python, but I don't know how to do this automatically for all of the tables.这很完美,我可以导入 python,但我不知道如何为所有表自动执行此操作。

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

Another option I found was saving everything, like so:我发现的另一个选择是保存所有内容,如下所示:

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

But this I cannot read into python, as it has unrecegnized objects.但是我无法读入 python,因为它有无法识别的对象。 I assume because of the functions that were defined in R.我假设是因为 R 中定义的功能。

To sum up, I would be very happy if someone could show me how to fill in the list of all of the tables into the first command.总而言之,如果有人能告诉我如何将所有表的列表填写到第一个命令中,我将非常高兴。 Or my other thought was if it is possible to delete functions and objects, so I can export only the required objects.或者我的另一个想法是是否可以删除函数和对象,所以我只能导出所需的对象。

Any help is greatly appreciated!任何帮助是极大的赞赏!

Many thanks!非常感谢!

There is this: Loading.RData files into Python and more modern perhaps, this: How to load R's.rdata files into Python?有这个: Loading.RData files into Python和更现代的可能,这个: How to load R's.rdata files into Python?

searching for python read rdata gave me lots of results.搜索python 读取 rdata给了我很多结果。

For simple structures json is otherwise nice.对于简单的结构 json 在其他方面很好。


library(jsonlite)

l <- list(
    cluster1, cluster2, cluster3
)

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

Then just read that instead.然后改为阅读。 Python reads json for breakfast. Python 读取 json 作为早餐。

I noticed this does not preserve column names.我注意到这不会保留列名。 (likely nor rownames): (可能也不是行名):

I don't have your data, so grabbing the poor flowers again:我没有你的数据,所以再次抓起可怜的花朵:

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]
  ]
]

Now if you convert those arrays to data.frame, you get this structure, it might be easier to work with in python?现在,如果您将那些 arrays 转换为 data.frame,您会得到这个结构,在 python 中使用它可能更容易吗?

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

becomes this:变成这样:


[
  [
    {
      "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