我有我嘗試使用rpy2在IPython的筆記本電腦來讀取一個相當標準的CSV數據集/ Rmagic:rpy2/Rmagic:無法讀取CSV數據文件
# R code
%load_ext rmagic
%R my.data <- read.csv("/Users/xxx/Documents/data.csv")
我得到這個錯誤:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-31-844400cf68c6> in <module>()
25 ####Chunk 1: Inputting and checking the data
---> 27 get_ipython().magic(u'R my.data <- read.csv("/Users/xxx/Documents/data.csv")')
28 get_ipython().magic(u'R summary(my.data)')
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
2162 magic_name, _, magic_arg_s = arg_s.partition(' ')
2163 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2164 return self.run_line_magic(magic_name, magic_arg_s)
2165
2166 #-------------------------------------------------------------------------
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
2088 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2089 with self.builtin_trap:
-> 2090 result = fn(*args,**kwargs)
2091 return result
2092
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/IPython/extensions/rmagic.pyc in R(self, line, cell, local_ns)
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
189 # but it's overkill for just that one bit of state.
190 def magic_deco(arg):
--> 191 call = lambda f, *a, **k: f(*a, **k)
192
193 if callable(arg):
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/IPython/extensions/rmagic.pyc in R(self, line, cell, local_ns)
579 if return_output and not args.noreturn:
580 if result != ri.NULL:
--> 581 return self.Rconverter(result, dataframe=False)
582
583 __doc__ = __doc__.format(
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/IPython/extensions/rmagic.pyc in Rconverter(Robj, dataframe)
113 return np.asarray(Robj)
114 Robj = np.rec.fromarrays(Robj, names = names)
--> 115 return np.asarray(Robj)
116
117 @magics_class
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
233
234 """
--> 235 return array(a, dtype, copy=False, order=order)
236
237 def asanyarray(a, dtype=None, order=None):
TypeError: __float__ returned non-float (type rpy2.rinterface.NAIntegerType)
我猜這事做與NA值在我的CSV數據。我實際上並沒有在那裏寫一個值 - 只是一個空白條目(例如1,3,4)。
我試圖用NA,空格,0等代替空白條目 - 我總是得到相同的錯誤。我究竟做錯了什麼?
編輯:我試着用純rpy2做(而不進行任何更改我的數據集):
import rpy2.robjects as robjects
myData = robjects.r['read.csv']("/Users/xxx/Documents/data.csv")
print robjects.r['summary'](myData)
,它工作正常!所以這必須是IPython/Rmagic的東西。
假設csv文件在類型上是同類的,看起來有點大膽。可能更直觀的做法是將當前的* - dataframe *選項設爲默認選項,並創建一個新選項,例如「--homogeneous」? – lgautier