【我初学R时遇到的十个错误问题】

学习R中遇到的10个错误问题


错误案例 1

> load("gse73614.RData")
	 Error in load("gse73614.RData") : 
 	    ReadItem: unknown type 189, perhaps written by later version of R ````
 	    

解决方案

错误原因:R需要更高版本才能运行“load”,
但是经检查确认是最新版,且打开工作目录发现文件在工作目录下,再次更新R与RStudio,
发现错误同样未解决,通过RStudio打开我的工作目录发现未找到文件,所以得出的结果是
文件在我的工作目录下已近损坏,RStudio无法打开,报错,重新替换为新文件后,错误解决


错误案例 2

tf<-tempexp&tempgpl  #tempexp与tempgpl为一个向量
f1<-function(t1,t2)
{
  for(i in 1:length(tempexp))
  {
    x<-grep(t[i],t2)   #得到逻辑值放到x向量中
  }
  for(i in 1:l)   #求t1在t2中的个数
  {
    ifelse(x[i]=="TRUE",1,0)  
  }
  return(sum(x))
}
f1(tempexp,tempgpl)

运行后显示结果

Error in (function (cond) : 
在为'grep'函数选择方法时评估'pattern'参数出了错: object of type 'closure' is not subsettable

解决方案

错误原因:grep()参数pattern出错了,查询的grep()使用方法如下:

grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE,
     fixed = FALSE, useBytes = FALSE, invert = FALSE)

pattern为单个字符,不可以为向量


错误案例 3

f<-C(1:10,5:150)
f1<-factor(f)

运行后显示结果

> f<-C(1:10,5:150)
Error in C(1:10, 5:150) : 不能把对象解释成因子

解决方案

错误原因是:定义向量 f时,使用了大写的 c,导致报错

方案:改为小写后则正确


错误案列 4

x3<-grep(tempexp,tempgpl) #tempexp与tempgpl为长度相同,内容类型相同的向量,内容为字符型

运行后显示结果

Warning message:
In grep(tempexp, tempgpl) :
  argument 'pattern' has length > 1 and only the first element will be used

解决方案

错误原因是:pattern只能是一个元素

方案:应使用for()函数,一个一个提取tempexp一个一个对比,如下:

f<-function(x)
{
	for(i in 1:length(tempexp))
	{
 	  x3[i]<-c(grep(tempexp[i],tempgpl),i) 
	}
    return(x3)
}

错误案例 5

 stewd("F://R.cx")

运行后显示结果

Error in stewd("F://R.cx") : could not find function "stewd"

解决方案

错误原因:stewd未找到

方案:写错了,重新拼写


错误案例 6

load("GSE73614.Rdata")

运行后显示的结果

Error in readChar(con, 5L, useBytes = TRUE) : cannot open the connection
In addition: Warning message:
In readChar(con, 5L, useBytes = TRUE) :
  cannot open compressed file 'GSE73614.Rdata', probable reason 'No such file or directory'

解决方案

错误原因:数据包"GSE73614.Rata未找到,可能是不在工作目录下

方案:更改数据包的位置,使用一下函数改到工作目录下

stewd("工作目录全路径") #注意斜杠为双数

错误案例 7

install.packages(ggplot2)

运行后显示的结果

Error in install.packages : object 'ggplot2' not found

解决方案

错误原因:ggplot2未找到。

方案:加上双引号,如下:

install.packages("ggplto2")

错误案例 8

i<-2
S<-sprintf(“the square of %d is %d”,i,i^2)

运行后显示的结果

Error: unexpected input in "S<-sprint(“"

解决方案

错误原因:未识别到引号内容是啥,反复确认后观察出使用了中文引号

方案:改为英文引号,如下:

S<-sprintf("the square of %d is %d",i,i^2)

错误案例 9

x<-sample(c(0,1),100,T) #随机0,1生成100个
F<-function(X,k)   #向量X中连续k个0或者1的位置
{
  L<-length(X)
  Site0<-NULL
  Site1<-NULL
  for (i in 1:(L-k+1))
  {
    if(all(X[i:(i+k-1)]==0))  
    {
      Site0<-c(Site0,i)   #有k个0的位置
    }
  }
  for (i in 1:(L-k+1))
  {
    if(all(X[i:(i+k-1)]==1))  
    {
      Site1<-c(Site1,i)    #有k个1的位置
    }
  }
  return(Site0,Site1)    #返回连续有k个0与1的位置
} 
F(x,3)

运行后显示的结果

Error in return(Site0, Site1) : multi-argument returns are not permitted

解决方案

错误原因:此操作下return不可多参数返回

方案:使用向量返回参数,如下:

return(c(Site0,Site1))

错误案例 10

f<-function()
{
  s<-NULL
  l<-length(x)
  for(i in 1:l)
  {
    ifelse(x[i]=="TRUE",1,0)
  }
  return(sum(x))
}
f(tf)

运行后显示的结果

Error in f(tf) : unused argument (tf)

解决方案

错误原因:tf使用错误,发现function后括号内无参数,应加一个参数x

f<-function(x){...}

以上代码为平时学习R时遇到的问题以及解决方法,如有错误,欢迎批评指正


猜你喜欢

转载自blog.csdn.net/qq_74093550/article/details/128323450