1

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.

one of the couple hundred tables I need to export

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:

enter image description here

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!

1 Answers1

1

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
    }
  ]
]
Sirius
  • 5,224
  • 2
  • 14
  • 21
  • Thank you! Could you tell me how I can add all the clusters automatically that list? For example in the analysis I was running now I have 535. The way i tried was mylist <- rep(NA,535) and then replacing each element in that using a for loop with cluster+i. But that created a set of characters only! – Gergely Szarka Mar 28 '21 at 21:19
  • 1
    sure: `l <- lapply( 0:534, function(i){ get(paste0("cluster",i)) } )` # should do the trick, I'm assuming they are called cluster0 - cluster534 – Sirius Mar 28 '21 at 21:24
  • note I had one `list()` too much in there, this is not necessary, lapply already takes care of that. – Sirius Mar 28 '21 at 21:28