Reputation: 11
I am working on a couple of small datasets, 200-250 observations each. I am interested in analyzing the likert scale (six point) data contained in specific columns of each of these datasets. The variables I am interested are named 'effectiveness', 'potential for scaling up' and 'sustainability'. One of the things I am trying to do is to do a correlation analysis between various variables in these datasets.
However, I am getting the following error message when using the cor.test function - "Error in cor.test.default(PCRsubset2006_15$Sustainability, PCRsubset2006_15$Potential.for.Scaling.up, : 'y' must be a numeric vector" .
I have tried using the as.numeric to force the said variables to be taken as a numeric vector. I have tested the code multiple times and I find that the issue seems to lie with the 'potential for scaling up' vector. The error seems to show up when that particular column is included in the code. When I apply is.numeric() to that vector it returns 'False'
My code is as below:
PCRsubset2006_10 <- subset.data.frame(PCRs.ratings, PCRs.ratings$Year.of.PCR.review > 2005 & PCRs.ratings$Year.of.PCR.review < 2011)
PCRsubset2011_15 <- subset.data.frame(PCRs.ratings, PCRs.ratings$Year.of.PCR.review > 2010 & PCRs.ratings$Year.of.PCR.review < 2016)
PCRsubset2006_15 <- subset.data.frame(PCRs.ratings, PCRs.ratings$Year.of.PCR.review > 2005 & PCRs.ratings$Year.of.PCR.review < 2016)
as.numeric(PCRsubset2006_15$Effectiveness)
as.numeric(PCRsubset2006_15$Potential.for.Scaling.up)
as.numeric(PCRsubset2006_15$Sustainability)
is.numeric(PCRsubset2006_15$Effectiveness)
is.numeric(PCRsubset2006_15$Potential.for.Scaling.up)
cor.test(PCRsubset2006_15$Effectiveness, PCRsubset2006_15$Sustainability, method = "spearman",exact = FALSE)
cor.test(PCRsubset2006_15$Sustainability, PCRsubset2006_15$Potential.for.Scaling.up, method = "spearman",exact = FALSE, use = "complete.obs")
I have tried to find a similar question here but can't seem to find a solution. Any help or indication in this regard for a beginner in coding would be of immense help.
Upvotes: 1
Views: 48
Reputation: 950
is.numeric()
tests whether it is a numeric type or not. FALSE
means it is not. To save the variable as a numeric type you need to do this:
PCRsubset2006_15$Potential.for.Scaling.up < - as.numeric(PCRsubset2006_15$Potential.for.Scaling.up)
Now if you used the cor
function it should work. An alternative is to do the following:
cor.test(PCRsubset2006_15$Effectiveness, as.numeric(PCRsubset2006_15$Potential.for.Scaling.up), method = "spearman",exact = FALSE)
Upvotes: 0