简体   繁体   中英

Passing python list as argument to R function in rpy2

I am starting with rpy2 with python for data analysis. I can pass simple python variable(like z=2.534) to R function in rpy2 as:

value=robjects.r('''
f<-function(z){
# process and return something
}
f('''+str(z)+''')
''')

But, when I pass list for z like z=[1,2,3] for above code str(z) does not work and when I manually pass '[1,2,3]' for str(z) ,function f would obviously be unable to work with character data as the list in R.

I need to find mean of items in list like [1,2,3] so how can this be done?

The documentation on evaluating R code suggests:

f = robjects.r('''
f<-function(z){
    # process and return something
}''')

value = f(z)

Try something like this. There is a FloatVector and StrVector as well.

res = robjects.IntVector([1, 2, 3])

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