1. rpy2
  2. rpy2
  3. rpy2
  4. Issues

Issues

Issue #392 resolved

the constructor for DataFrame in rpy2 doesn't expose the parameter "stringsAsFactors"

Fabien Tarrade
created an issue

Hi there,

It suggested to me on stack overflow to open an issue about it.

The constructor for DataFrame in rpy2 doesn't expose the parameter

stringsAsFactors 

available for the R function data.frame.

The trick suggested by lgautier is to do the following in Python:

from rpy2.robjects import pandas2ri
from rpy2.robjects.packages import importr

base = importr('base')
pandas2ri.activate()
...

# CONVERT PANDAS DF TO R DF
rdf = pandas2ri.py2ri(pydf)

# FIND COLUMN INDEX OF EACH FACTOR IN DF
factors = [i for i,col in enumerate(rdf) if col.rclass[0] == 'factor']

# CONVERT COLS ITERATIVELY
for f in factors:
    rdf[f] = base.as_character(rdf[f])

or I found that the following is working in R:

i <- sapply(df, is.factor)
df[i] <- lapply(df[i], as.character)

It will be nice instead to pass the argument

stringsAsFactors

to the converter.

Thanks a lot Cheers Fabien

Comments (6)

  1. Fabien Tarrade reporter

    Hi Laurent, thanks a lot. I have a simple question. So far I can only fine one version with conda: conda install -c r rpy2=2.8.2 is it expected ? Is the normal way to download the zip file from bitbucket and then install it with conda or is there another channel with the recent version ?

    Thanks Cheers Fabien

  2. Laurent Gautier

    Using pip to install should work well if you have a build environment (C compiler, required library/headers). There is also a Docker container.

    Check the project's page (http://rpy2.bitbucket.org/) and links to the documentation.

    If no build environment, or Docker not practical, conda should work (but I am not a specialist of). The latest rpy2 release is 2.8.5.

  3. Log in to comment