【发布时间】:2021-07-11 23:48:08
【问题描述】:
编辑:我已经多次编辑代码但仍然收到此错误返回错误 data.frame(..., check.names = FALSE) : 参数暗示不同的行数:2、75
我正在 R 中从 excel 数据表中构建向量,然后尝试创建一个具有这些向量之间角度的新文件。我对最后几行代码感到有些困惑,其中两个向量之间角度的计算被插入到一个新文件中(原始 .csv 在 wd 中用于获取 MonardaME 和硫向量数据)。
> d<- read.csv(file.choose(), header=T,stringsAsFactors = FALSE)
> attach(d)
> MonardaME
[1] 0.000 0.000 41.500 0.000 0.000 0.000 21.685 0.000 0.000 21.535
[11] 0.565 0.000 38.515 9.055 0.000 0.000 20.300 47.020 0.000 0.000
[21] 0.000 0.000 1.945 0.000 0.690 0.000 75.740 0.000 0.000 0.565
[31] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[41] 8.530 79.275 1.945 0.000 0.205 0.000 9.210 0.270 0.000 0.000
[51] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[61] 0.000 0.000 0.000 0.000 0.000 79.275 9.095 0.000 0.000 0.000
[71] 0.000 0.000 0.000 0.000
> sulfur
[1] 0.000 0.000 89.220 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[11] 0.000 0.000 49.815 4.995 0.000 0.000 38.055 0.000 0.000 0.000
[21] 0.000 44.820 5.070 0.000 0.000 0.000 35.465 0.000 5.070 0.000
[31] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 7.920
[41] 35.465 0.000 0.000 0.000 0.000 0.000 3.940 0.000 0.000 0.000
[51] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[61] 0.000 0.000 3.120 0.000 2.590 80.285 0.000 0.000 0.000 0.000
[71] 0.000 4.995 0.000 0.000
> as.numeric(sulfur)
[1] 0.000 0.000 89.220 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[11] 0.000 0.000 49.815 4.995 0.000 0.000 38.055 0.000 0.000 0.000
[21] 0.000 44.820 5.070 0.000 0.000 0.000 35.465 0.000 5.070 0.000
[31] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 7.920
[41] 35.465 0.000 0.000 0.000 0.000 0.000 3.940 0.000 0.000 0.000
[51] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[61] 0.000 0.000 3.120 0.000 2.590 80.285 0.000 0.000 0.000 0.000
[71] 0.000 4.995 0.000 0.000
> as.numeric(MonardaME)
[1] 0.000 0.000 41.500 0.000 0.000 0.000 21.685 0.000 0.000 21.535
[11] 0.565 0.000 38.515 9.055 0.000 0.000 20.300 47.020 0.000 0.000
[21] 0.000 0.000 1.945 0.000 0.690 0.000 75.740 0.000 0.000 0.565
[31] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[41] 8.530 79.275 1.945 0.000 0.205 0.000 9.210 0.270 0.000 0.000
[51] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[61] 0.000 0.000 0.000 0.000 0.000 79.275 9.095 0.000 0.000 0.000
[71] 0.000 0.000 0.000 0.000
> focalScent<-readline(prompt="MonardaME")
MonardaME
> focalScentName=focalScent
> samples=colnames(d)
> a=match('dimensions', samples)
> samples=samples[-a]
> a=match(focalScent, samples)
> samples=samples[-a]
> d2<-d[,-1]
> rownames(d2)<-d[,-1]
Error in `row.names<-.data.frame`(`*tmp*`, value = value) :
invalid 'row.names' length
> rownames(d2)<-d[,1]
> attach(d2)
The following objects are masked from d:
MonardaME, MonardaMErun1, MonardaMErun2, sulfur, sulfurRun1,
sulfurRun2
> angleData=c(0,0)
> anglefileName<-"MonardaSulfur"
> anglefileName<-"MonardaSulfur"
> MonardaSulfur
Error: object 'MonardaSulfur' not found
> "MonardaSulfur"
[1] "MonardaSulfur"
> anglefileName<-"MonardaSulfurAngle"
> MonardaSulfurAngle
Error: object 'MonardaSulfurAngle' not found
> "MonardaSulfurAngle"
[1] "MonardaSulfurAngle"
> anglefileName
[1] "MonardaSulfurAngle"
> write.table(angleData,file=anglefileName,append=FALSE,row.names = FALSE,col.names=FALSE)
> assign("vec1", c(MonardaME))
> vec1
[1] 0.000 0.000 41.500 0.000 0.000 0.000 21.685 0.000 0.000 21.535
[11] 0.565 0.000 38.515 9.055 0.000 0.000 20.300 47.020 0.000 0.000
[21] 0.000 0.000 1.945 0.000 0.690 0.000 75.740 0.000 0.000 0.565
[31] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[41] 8.530 79.275 1.945 0.000 0.205 0.000 9.210 0.270 0.000 0.000
[51] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[61] 0.000 0.000 0.000 0.000 0.000 79.275 9.095 0.000 0.000 0.000
[71] 0.000 0.000 0.000 0.000
> assign("vec2", c(sulfur))
> vec2
[1] 0.000 0.000 89.220 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[11] 0.000 0.000 49.815 4.995 0.000 0.000 38.055 0.000 0.000 0.000
[21] 0.000 44.820 5.070 0.000 0.000 0.000 35.465 0.000 5.070 0.000
[31] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 7.920
[41] 35.465 0.000 0.000 0.000 0.000 0.000 3.940 0.000 0.000 0.000
[51] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
[61] 0.000 0.000 3.120 0.000 2.590 80.285 0.000 0.000 0.000 0.000
[71] 0.000 4.995 0.000 0.000
> vec1=MonardaME
> for(i in samples){}
> for(i in samples){
+ a=vec1*vec1
+ vectorLength1=sqrt(sum(a))
+ b=vec2*vec2
+ vectorLength2=sqrt(sum(b))
+ dotVectors=vec1%*%vec2
+ Theta=acos(dotVectors/(vectorLength1*vectorLength2))
+ angleName= paste(MonardaME, i, sep="")
+ angleData<-c(angleName, Theta)
+ temporary<-read.table(anglefileName, header=FALSE, sep="")
+ temporary2<-cbind.data.frame(temporary,angleData)
+ write.table(temporary2,file=anglefileName,append=FALSE,sep=",")
+ }
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 2, 75
【问题讨论】: