Vinkebot
Vinkebot

Reputation: 11

Error: "Error in 1:ncol(y) : argument of length 0" when attempting correlation analysis

I am reading in a csv file consisting of two columns of data from two sources.I would like to find the p values between them, however, I get the error mentioned in the title.

library(psych)
RfileX = read.csv(fpath, header = TRUE)
x = as.matrix(RfileX)
a=x[1:52,1]
b=x[1:52,2]
print(corr.test(a,b, adjust = "none"), short = FALSE)

Data for reference (What do the Ls mean?, thanks)

structure(list(A1 = c(2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 3L), 
    B1 = c(3L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 2L)), .Names = c("A1", 
    "B1"), row.names = c(NA, 10L), class = "data.frame")

Upvotes: 0

Views: 3779

Answers (1)

r2evans
r2evans

Reputation: 160447

Assuming the psych package.

If you read ?corr.test, you'll see that the first two arguments are:

   x: A matrix or dataframe

   y: A second matrix or dataframe with the same number of rows as
      x

Not vectors. So, you should be able to run corr.test(RfileX, ...), ala:

library(psych)
set.seed(42)
x <- data.frame(a = sample(2:3, size = 100, replace = TRUE),
                b = sample(2:3, size = 100, replace = TRUE))
print(corr.test(x, adjust = "none"), short = FALSE)
# Call:corr.test(x = x, adjust = "none")
# Correlation matrix 
#      a    b
# a 1.00 0.13
# b 0.13 1.00
# Sample Size 
# [1] 100
# Probability values (Entries above the diagonal are adjusted for multiple tests.) 
#     a   b
# a 0.0 0.2
# b 0.2 0.0
#  To see confidence intervals of the correlations, print with the short=FALSE option
#  Confidence intervals based upon normal theory.  To get bootstrapped values, try cor.ci
#     lower    r upper   p
# a-b -0.07 0.13  0.32 0.2

Upvotes: 2

Related Questions