Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting rid of "AsIs" class attribute

Tags:

dataframe

r

Simple question I think but have not found an answer. How to a get rid of the "AsIs" class attribute on my data frame. It is preventing the write.dbf from the foreign package from converting to dbf. I am working from rpy2 but it does work with R data frame with no "AsIs". I put the full code below the error message. dbfs = write_dbf(r_dataframe)

Error in function (dataframe, file, factor2char = TRUE, max_nchar = 254)  : 
  data frame contains columns of unsupported class(es) AsIs

---------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
<ipython-input-26-9072df63231a> in <module>()
----> 1 dbfs = write_dbf(r_dataframe)

/home/matthew/.virtualenvs/mypython/lib/python3.2/site-packages/rpy2-2.2.6dev_20120814-py3.2-linux-i686.egg/rpy2/robjects/functions.py in __call__(self, *args, **kwargs)
     80                 v = kwargs.pop(k)
     81                 kwargs[r_k] = v
---> 82         return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)

/home/matthew/.virtualenvs/mypython/lib/python3.2/site-packages/rpy2-2.2.6dev_20120814-py3.2-linux-i686.egg/rpy2/robjects/functions.py in __call__(self, *args, **kwargs)
     32         for k, v in kwargs.items():
     33             new_kwargs[k] = conversion.py2ri(v)
---> 34         res = super(Function, self).__call__(*new_args, **new_kwargs)
     35         res = conversion.ri2py(res)
     36         return res

RRuntimeError: Error in function (dataframe, file, factor2char = TRUE, max_nchar = 254)  : 
  data frame contains columns of unsupported class(es) AsIs

I am using python rpy2 to talk to R. That is not where the problem is but here is my code. The write.dbf works from Rpy2 if I use a data frame from R with no "AsIs".

(python)

    df = DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C':[7,8,9]},index=["one", "two", "three"])  I am going from python pandas dataframe to and R datafram using 

    fore = importr("foreign")

    In [19]:

    r_dataframe = com.convert_to_r_dataframe(df)

    In [20]:

    print(type(r_dataframe))

    <class 'rpy2.robjects.vectors.DataFrame'>

    In [32]:

    r_dataframe

    Out[32]:

<DataFrame - Python:0xb3db8ac / R:0xc23ac50>
[IntVector, IntVector, IntVector]
  A: <class 'rpy2.robjects.vectors.IntVector'>
  <IntVector - Python:0xc1fb1ec / R:0xc23ac28>
[       1,        2,        3]
  B: <class 'rpy2.robjects.vectors.IntVector'>
  <IntVector - Python:0xc1fb36c / R:0xc23ac00>
[       4,        5,        6]
  C: <class 'rpy2.robjects.vectors.IntVector'>
  <IntVector - Python:0xc1fb4ec / R:0xc23abd8>
[       7,        8,        9]


    print(r_dataframe)

          A B C
    one   1 4 7
    two   2 5 8
    three 3 6 9

    In [25]:

    write_dbf =robjects.r("write.dbf")

    read_dbf = robjects.r("read.dbf")

    In [26]:

    dbfs = write_dbf(r_dataframe)

Error in function (dataframe, file, factor2char = TRUE, max_nchar = 254)  : 
  data frame contains columns of unsupported class(es) AsI

    dbfs = write_dbf(r_dataframe)
like image 781
user1246428 Avatar asked Oct 12 '12 18:10

user1246428


People also ask

What is AsIs class in R?

Class "AsIs" has a few of its own methods, including for [ , as. data. frame , print and format . In function formula . There it is used to inhibit the interpretation of operators such as "+" , "-" , "*" and "^" as formula operators, so they are used as arithmetical operators.

Why would you use a data frame over a vector to store your data?

One difference is that if we try to get a single row of the data frame, we get back a data frame with one row, rather than a vector. This is because the row may contain data of different types, and a vector can only hold elements of all the same type. Internally, a data frame is a list of column vectors.


1 Answers

Here's how I would get rid of an AsIs class attribute. Notice that I take care to preserve any other class attributes that the vector might have:

unAsIs <- function(X) {
    if("AsIs" %in% class(X)) {
        class(X) <- class(X)[-match("AsIs", class(X))]
    }
    X
}

## Show why the function is needed
a <- 1:10
b <- factor(1:10)

class(I(a))
# [1] "AsIs"
class(I(b))
# [1] "AsIs"   "factor"

## Show that the function reverses the effect of `I()`
identical(a, unAsIs(I(a)))
# [1] TRUE
identical(b, unAsIs(I(b)))
# [1] TRUE
like image 103
Josh O'Brien Avatar answered Oct 19 '22 23:10

Josh O'Brien