简体   繁体   中英

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

These table objects have the following 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. Then I found an option where I can export objects in to 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.

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. I assume because of the functions that were defined in 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?

searching for python read rdata gave me lots of results.

For simple structures json is otherwise nice.


library(jsonlite)

l <- list(
    cluster1, cluster2, cluster3
)

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

Then just read that instead. Python reads json for breakfast.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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