i’m writing a script that reads two .txt file in two vectors. After that I want to make a Spearman’s rank correlation and plot the result.
The first vectors value’s length is 12-13 characters (e.g. 7.3445555667 or 10.3445555667) and the second vectors value’s length is one character (e.g. 1 or 2).
The code:
vector1 <- read.table ("D:...path.../mytext1.txt", header=FALSE)
vector2 <- read.table ("D:...path.../mytext2.txt", header=FALSE)
cor.coeff = cor(vector1 , vector2 , method = "spearman")
cor.test(vector1 , vector2 , method = "spearman")
plot(vector1.var, vector2.var)
The .txt files contain only numeric values.
I’m getting two errors, the first in line 4 it’s like ” ‘x’ have to be a numeric vector”
and the second error occurs in line 5 it’s like “object vector 1.var couldn’t be found”
I also tried
plot(vector1, vector2)
instead of
plot(vector1.var, vector2.var)
But then there’s an error like “Error in stripchart.default (x1,…) : invalid plot-method
The implementation is orientated at http://www.gardenersown.co.uk/Education/Lectures/R/correl.htm#correlation
I doubt
vector1andvector2are vectors. Reading?read.tablewe note in the Value section:So even if your two text files contain just a single variable, the two objects read in will be data frames with a single component each.
Secondly, your data files don’t contain headers so R will make up a variable name. I haven’t tested this but IIRC your the variables in
vector1andvector2will both be calledX1. Dohead(vector1)and the same onvector2(ornames(vector1)) to see how your objects look in R.I can see why you might think
vector1.varmight work, but you should realise that as far as R was concerned it was looking for an object namedvector1.var. The.is just any other character in R object names. If you meant to use.as a subsetting or selection operator, then you need to read up on subsetting operators in R. These are$and[and[[. See for example the R Language Definition manual or the R manual.I suspect you could just change your code to:
But I am supposing quite a bit about what is in your two text files…