【问题标题】:Building vectors/calculating dot products in r在 r 中构建向量/计算点积
【发布时间】: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

【问题讨论】:

    标签: r excel csv vector


    【解决方案1】:

    您的代码生成ErrorWarningError 会破坏您的代码,Warning 是您已经意识到的问题,但它不会破坏您的代码。

    错误信息no lines available in input 表示您正在尝试读取的文件存在,但它是空的。修复此问题将使您的代码运行。

    警告消息file("") only supports open = "w+" and open = "w+b": using the former 来自write.table() 调用和append 参数设置为FALSE。更改此项应该会使警告消失,但不会对代码的结果产生任何影响。

    我很难弄清楚到底发生了什么,因为我没有您的原始 csv 文件可用。似乎anglefileName&lt;-readline(prompt = "MonardaSulfurAngle") 行可能是您最后收到错误消息的原因。请改用anglefileName &lt;- "MonardaSulfurAngle"

    另外请注意,您之前还有一些其他错误消息。其中一个是由于拼写错误(samplse 应该是 samples)。

    【讨论】:

    • 非常感谢!原始 .csv 文件包含我用来制作矢量的数据,但不确定为什么我尝试使用角度数据创建的新文件是空的,实际上它甚至没有出现在我的工作驱动器中......跨度>
    • 那是因为你给它的名字是一个空字符串。在控制台中输入anglefileName,它将返回""
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 2012-07-04
    • 1970-01-01
    • 2014-03-01
    • 2017-10-06
    • 2010-09-19
    相关资源
    最近更新 更多