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

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

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

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

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

報錯信息:

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

報錯原因:

數(shù)據(jù)不是可計算的 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)
# 出現(xiàn)報錯
Error in y + 1 : non-numeric argument to binary operator

報錯原因解析:

1. 轉(zhuǎn)置后數(shù)據(jù)類型變?yōu)閏haracter,因為numeric數(shù)據(jù)中存在character類型的臟數(shù)據(jù)

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

如何發(fā)現(xiàn)是否有character臟數(shù)據(jù):

read.table設(shè)置參數(shù)colClasses = “numeric”(確保數(shù)據(jù)框內(nèi)只有numeric類型)

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

報錯意為 數(shù)據(jù)框內(nèi)存在“f__Cenarchaeaceae”,不屬于numeric

查看txt內(nèi)部

2. 引入character臟數(shù)據(jù)的原因

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

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

總結(jié)

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

欄目分類
最近更新