列引用[,1]有效,但列名x $ col1不可用

时间:2015-11-16 15:23:55

标签: r

x[,1]x$col1之间有什么区别?下面的spcor函数适用于第一个选项,但是第二个选项失败。如果我将矩阵转换为数据帧,这两个选项都有效。

library(matrixcalc)
library(ppcor)
exampledf<- matrix(ceiling(runif(16,0,50)), ncol=4)
while(is.singular.matrix(exampledf)!=TRUE){
  exampledf<- matrix(ceiling(runif(16,0,50)), ncol=4)
}
colnames(exampledf)<-c("col1","col2","col3","col4")

x<-exampledf
x<-x[complete.cases(x),] #semi-partial func below requires complete cases
spcor.test(x$col1,x$col2,x$col3) #doesnt work unless cast to data.frame for some reason
spcor.test(x[,1],x[,2],x[,3]) #works fine

0 个答案:

没有答案