gpt4 book ai didi

r - 摆脱 "AsIs"类属性

转载 作者:行者123 更新时间:2023-12-02 07:37:11 26 4
gpt4 key购买 nike

我认为简单的问题但尚未找到答案。如何摆脱数据框中的“AsIs”类属性。它阻止 foreign 包中的 write.dbf 转换为 dbf。我正在使用 rpy2,但它确实适用于没有“AsIs”的 R 数据帧。我把完整的代码放在错误消息下面。 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

我正在使用 python rpy2 与 R 对话。这不是问题所在,但这是我的代码。如果我使用 R 中没有“AsIs”的数据帧,则 write.dbf 可以从 Rpy2 运行。

( 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)

最佳答案

以下是我如何删除 AsIs 类属性。请注意,我注意保留向量可能具有的任何其他类属性:

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

关于r - 摆脱 "AsIs"类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12865218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com