Natalie David
Natalie David

Reputation: 21

Building vectors/calculating dot products in r

Edit: I have edited the code many times yet still get this error in return Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 2, 75

I am building vectors in R from an excel sheet of data, then trying to create a new file with the angle between these vectors. I am a little confused by the last few lines of code where the calculation of the angle between the two vectors gets plugged into a new file (original .csv is in wd used to get the MonardaME and sulfur vector data).

> 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

Upvotes: 0

Views: 44

Answers (1)

Till
Till

Reputation: 6663

Your code produces an Error and a Warning. The Error is what breaks your code, the Warning is an issue you are made aware of, but it is not breaking your code.

The error message no lines available in input means that the file you are trying to read exists, but it is empty. Fixing this will make your code run.

The warning message file("") only supports open = "w+" and open = "w+b": using the former comes from the write.table() call and the append argument being set to FALSE. Changing this should make the warning go away, but won't make any difference to the results of your code.

It's a bit hard for me to figure out exactly what is going on, since I don't have your original csv file available. It seems though that the line anglefileName<-readline(prompt = "MonardaSulfurAngle") could be the reason for the error messages you get at the end. Try anglefileName <- "MonardaSulfurAngle" instead.

Also be aware that you have a few other error messages earlier. One of them is due to a typo (samplse should be samples).

Upvotes: 1

Related Questions