日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

解決R語言報錯:Error?in?y?+?1:non-numeric?argument?to?binary?operator_R語言

作者:白爾特 ? 更新時間: 2022-12-06 編程語言

因為花了2天半才解決,中間痛苦的尋找,記錄一下解決的流程與經驗

報錯信息:

1Error in y + 1 : non-numeric argument to binary operator

報錯原因:

數據不是可計算的 numeric 或 integer 類型

原代碼:

圖片描述

a = read.table(file = study.txt", sep = "\t",
  header = T, row.names = 1
  )
class(a[3, 3])    # integer
aa = t(d)
class(aa[3, 3])   # character
b = sparcc(aa)
# 出現報錯
Error in y + 1 : non-numeric argument to binary operator

報錯原因解析:

1. 轉置后數據類型變為character,因為numeric數據中存在character類型的臟數據

(原因:轉置函數t() 是先將dataframe轉換為矩陣matrix,而matrix只有一種數據類型。所以如果存在character,所有數據都會被轉換成character)

如何發現是否有character臟數據:

read.table設置參數colClasses = “numeric”(確保數據框內只有numeric類型)

a = read.table(file = study.txt", sep = "\t",
  header = T, row.names = 1
  colClasses = "numeric"   # 添加的參數
  )
  
  # 出現報錯
  Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  : 
  scan() expected 'a real', got 'f__Cenarchaeaceae'

報錯意為 數據框內存在“f__Cenarchaeaceae”,不屬于numeric

查看txt內部

2. 引入character臟數據的原因

# 后續分析需要:設置data第一列列名為空格
genus <- data[1]
colnames(genus) <- " "
# 根據列名提取子集
 a <- subset(data, select = (disID[, 1]))

subset()函數將列名為 空格blank 的也提取了,導致了character臟數據的進入

總結

原文鏈接:https://blog.csdn.net/m0_48412773/article/details/125759996

欄目分類
最近更新