天天看點

【R】 Error in is.data.frame(x) : (list) object cannot be coerced to type 'double'

對于不經常使用R語言進行程式設計的人來說,一接觸R語言可能會遇到各種各樣的問題。今天在使用R語言讀取資料的時候遇到了如下的錯誤:

Error in is.data.frame(x) : 
  (list) object cannot be coerced to type 'double'
           

讀取資料的代碼如下:(其中interval_estimated()是自己定義的一個方法)

df <- read.csv("C://Users//Machenike//Desktop//zzz//data.csv", header = FALSE, sep = "\t")
interval_estimated(df[3])
Error in is.data.frame(x) : 
  (list) object cannot be coerced to type 'double'
In addition: Warning message:
In mean.default(x) :
 Show Traceback
 
 Rerun with Debug
 Error in is.data.frame(x) : 
  (list) object cannot be coerced to type 'double'
           

資料内容如下:

head(df)
           V1 V2         V3
1 1.01121e+19  1 0.03000000
2 1.01121e+19  1 0.03000000
3 1.01121e+19  1 0.03000002
4 1.01121e+19  1 0.00000000
5 1.01121e+19  1 0.03000000
6 1.01121e+19  1 0.00000000
           

解決辦法:

是因為讀入的是資料框形式的,此處調用不應該使用df[3],應使用df[,3],如下:

interval_estimated(df[,3])
       mean     df          a          b
1 0.0314915 404901 0.02930638 0.03367661
           

繼續閱讀