translating named list vectors from R into rpy2 in Python?

2024/10/6 12:27:56

What is the equivalent of the following R code in Rpy2 in python?

Var1 = c("navy", "darkgreen")
names(Var1) = c("Class1", "Class2")
ann_colors = list(Var1 = Var1)

It's not clear what ann_colors is. When evaluated in R it gives:

> ann_colors
$Var1Class1      Class2 "navy" "darkgreen" 

Is it an robject.ListVector? I tried:

robjects.ListVector({"Class1": "navy", "Class2": "green"})

but it's not quite it because I'm not sure how to tell the ListVector object that the name of this object is Var1, i.e. something like list(Var1 = Var1).

How can this be properly translated to rpy2?

Answer

If I understand right your question what you are looking for is a TaggedList:

import rpy2.rlike.container as rlc
Var1 = rlc.TaggedList(["navy","darkgreen"], tags=('Class1', 'Class2'))

See http://rpy.sourceforge.net/rpy2/doc-2.2/html/rlike.html for more details.

https://en.xdnf.cn/q/70371.html

Related Q&A

Issue parsing multiline JSON file using Python

I am trying to parse a JSON multiline file using json library in Python 2.7. A simplified sample file is given below:{ "observations": {"notice": [{"copyright": "Copy…

timezone aware vs. timezone naive in python

I am working with datetime objects in python. I have a function that takes a time and finds the different between that time and now. def function(past_time):now = datetime.now()diff = now - past_timeWh…

How to return a value from Python script as a Bash variable?

This is a summary of my code:# import whateverdef createFolder():#someCodevar1=Gdrive.createFolder(name)return var1 def main():#someCodevar2=createFolder()return var2if __name__ == "__main__"…

How to align text to the right in ttk Treeview widget?

I am using a ttk.Treeview widget to display a list of Arabic books. Arabic is a right-to-left language, so the text should be aligned to the right. The justify option that is available for Label and o…

ImportError: cannot import name RemovedInDjango19Warning

Im on Django 1.8.7 and Ive just installed Django-Allauth by cloning the repo and running pip install in the apps directory in my webapp on the terminal. Now when I run manage.py migrate, I get this err…

How does Yahoo Finance calculate Adjusted Close stock prices?

Heres how Yahoo Finance apparently calculates Adjusted Close stock prices:https://help.yahoo.com/kb/adjusted-close-sln28256.htmlFrom this, I understand that a constant factor is applied to the unadjust…

Celery design help: how to prevent concurrently executing tasks

Im fairly new to Celery/AMQP and am trying to come up with a task/queue/worker design to meet the following requirements.I have multiple types of "per-user" tasks: e.g., TaskA, TaskB, TaskC. …

Google App Engine - Using Search API Python with list fields

Im using ndb.Model. The Search API has the following field classes:TextField : plain textHtmlField : HTML formatted textAtomField : a string which is treated as a single tokenNumberField : a numeric v…

Handling PyMySql exceptions - Best Practices

My question regards exception best practices. Ill present my question on a specific case with PyMySQL but it regards errors handling in general. I am using PyMySQL and out of the many possible exceptio…

Beautifulsoup find element by text using `find_all` no matter if there are elements in it

For examplebs = BeautifulSoup("<html><a>sometext</a></html>") print bs.find_all("a",text=re.compile(r"some"))returns [<a>sometext</a>] …