Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select columns from R dataframe in rpy2 in python?

I have a dataframe in rpy2 in python and I want to pull out columns from it. What is the rpy2 equivalent of this R code?

df[,c("colA", "colC")]

this works to get the first column:

mydf.rx(1)

but how can I pull a set of columns, e.g. the 1st, 3rd and 5th?

mydf.rx([1,3,5])

does not work. neither does:

mydf.rx(rpy2.robjects.r.c([1,3,5]))

like image 625
lgd Avatar asked Jul 19 '15 02:07

lgd


1 Answers

Alternatively, you can pass the R data frame into a Python pandas data frame and subset your resulting 1, 3, 5 columns:

#!/usr/bin/python
import rpy2
import rpy2.robjects as ro
import pandas as pd
import pandas.rpy.common as com

# SOURCE R SCRIPT INSIDE PYTHON 
ro.r.source('C:\\Path\To\R script.R') 

# DEFINE PYTHON DF AS R DF
pydf = com.load_data('rdf')
cols = pydf[[1,3,5]]
like image 93
Parfait Avatar answered Sep 20 '22 01:09

Parfait