斯皮尔曼的等级相关性

Tyz*_*zak 2 plot r correlation

我正在编写一个脚本,在两个向量中读取两个.txt文件.之后我想制作一个斯皮尔曼的等级相关并绘制结果.第一个向量值的长度为12-13个字符(例如7.3445555667或10.3445555667),第二个向量值的长度为一个字符(例如1或2).

代码:

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)
Run Code Online (Sandbox Code Playgroud)

.txt文件仅包含数字值.

我得到两个错误,第4行第一个就像"'x'必须是一个数字向量",第二个错误发生在第5行,就像"无法找到对象向量1.var"

我也试过了

 plot(vector1, vector2)
Run Code Online (Sandbox Code Playgroud)

代替

 plot(vector1.var, vector2.var)
Run Code Online (Sandbox Code Playgroud)

但是有一个错误,如"stripchart.default(x1,...)中的错误:无效的plot-method

实施方向见http://www.gardenersown.co.uk/Education/Lectures/R/correl.htm#correlation

Mai*_*ura 5

str?str应该经常使用的一个非常有用的函数(参见更多信息),尤其是验证R对象类型.快速str(vector1),str(vector2)并告诉您这些列是否被读取为字符而不是数字.如果是,则用于as.numeric(vector1)对每个向量中的数据进行类型转换.

此外,names(vector1)names(vector2)会告诉你的列名是什么,以及可能解决您的绘图问题.


Rei*_*son 5

我怀疑vector1并且vector2是矢量.阅读?read.table我们在价值部分中注意到:

值:

    A data frame (‘data.frame’) containing a representation of the
    data in the file.
Run Code Online (Sandbox Code Playgroud)

....

因此,即使您的两个文本文件只包含一个变量,读入的两个对象也将是每个都包含一个组件的数据框.

其次,您的数据文件不包含标题,因此R将组成变量名称.我没有测试这一点,但IIRC你的变量vector1,并vector2都将被调用X1.head(vector1)vector2(或names(vector1))上执行相同操作以查看对象在R中的外观.

我可以看出为什么你认为vector1.var可能有用,但你应该意识到,就R而言,它正在寻找一个名为的对象vector1.var.该.仅仅是R中的对象名称的任何其它字符.如果你想用.的子集或选择运营商,那么你需要在子集化运营阅读起来R.这些都是$[[[.例如,参见R语言定义手册R手册.

我怀疑您可以将代码更改为:

vector1 <- read.table ("D:...path.../mytext1.txt", header=FALSE)[, 1]
vector2 <- read.table ("D:...path.../mytext2.txt", header=FALSE)[, 1]
cor.coeff <- cor(vector1 , vector2 , method = "spearman")
cor.test(vector1 , vector2 , method = "spearman")
plot(vector1, vector2)
Run Code Online (Sandbox Code Playgroud)

但我想你的两个文本文件中有什么...