I am running a cor.test on two columns within a file/table.
tmp <- read.table(files_to_test[i], header=TRUE, sep="\t")
## Obtain Columns To Compare ##
colA <-tmp[compareA]
colB <-tmp[compareB]
# sctr = 'spearman cor.test result'
sctr <- cor.test(colA, colB, alternative="two.sided", method="spearman")
But I am getting this confounding error…
Error in cor.test.default(colA, colB, alternative = "two.sided", method = "spearman") :
'x' must be a numeric vector
the values in the columns ARE numbers but
is.numeric(colA) = FALSE
class (colA) = data.frame
What have I missed?
Put a comma before your selector. When you select in a data.frame object with a single indexing variable without a comma it extracts a column as a list element retaining type. Therefore, it’s still a data.frame. But, data.frame objects allow you to select using matrix style notation and then you would get a simple vector. So just change
to
I think this is more keeping with the spirit of the data.frame type than double brace (
[[) selectors, which will do something similar but in the spirit of the underlying list type. They also are unrelated to individual item and row selectors. So, in code that’s doing multiple kinds of things with the data.frame the double brace selectors stand out as a bit of an odd duck.