0

What is the difference between x[,1] and x$col1? The spcor function below works fine for the first option, but fails with the second. Both options work if I cast my matrix to a dataframe.

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
Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • Yeah, data.frames are lists, with cols accessible with `$`; matrices are not. Try `m[, "col1"]` instead maybe. – Frank Nov 16 '15 at 15:26
  • Thanks, will you propose that as an answer please? – Rilcon42 Nov 16 '15 at 15:28
  • I suspect it's a dupe (hard to find though, since it's probably titled something with "$"). You can answer, maybe citing the relevant documentation. – Frank Nov 16 '15 at 15:29
  • 1
    Maybe this one? http://stackoverflow.com/questions/5744694/r-extract-matrix-column-values-by-matrix-column-name – Frank Nov 16 '15 at 15:32
  • Definitely similar. Is the correct procedure in this case to close the question, or delete it? – Rilcon42 Nov 16 '15 at 15:34
  • I think closing is fine; someone else might word their question similarly to you and this serves as a "signpost" to the right answer if it's closed rather than deleted. – Frank Nov 16 '15 at 15:35

0 Answers0