Hi, for some reason i can't run the cor command, help.

The error message explains that the cor() function requires the x argument to be numeric. Your gender column has character values. You could recode that data so that Female = 1 and Male = 2 but I do not think the correlation value would be very useful. What are you trying to accomplish?

i want to see whether there is a correlation between make and female when it comes to cognitive competence i also want to run a t.test but not sure whether to run paired or unpaired t test. And how do i make Female = 1 and Male = 2?

x <- c("a", "a", "a", "a", "b", "b")
y <- c(1, 1, 1, 1, 2, 2)
cor(as.numeric(as.factor(x)), y)

thank u i copied your code and ran the cor it still gives me the same error

that's weird, can you show me your code please?

x <- c("Female", "Female", "Female", "Female", "Male", "Male")
y <- c(1, 1, 1, 1, 2, 2)
cor(as.numeric(as.factor(x)), y)

here is the output:
[1] 1

glad that it works now!

that particular code runs but the cor command is still giving the same error, saying that x must be numeric

Did you convert your character object into a factor and then into numeric?

yes using this : df9$gender <- as.factor(df9$gender)
df9$Cognitive <- as.factor(df9$Cognitive)

Cognitive is already numeric, indicated by the tsibble with dbl. Then you need to convert the factor object into numeric with as.numeric(as.factor(x)).

Its somewhat counterproductive to make Cognitive a factor. Factors are there to compute on character strings 'as though' they are integer representations. When you already have numbers just use them directly for computation.

1 Like

Will this work? I get .16. I suspect correlation is not the right tool though. Logistic regression?

datafile0 <- data.frame(gender=c("Female", "Male", "Female","Female","Female", "Male"),

                     cognitive=c(4.56, 6, 5.43, 2.88, 3.55, 3))

datafile0$gender <- ifelse(datafile0$gender == "Female", 0, 1)

cor(datafile0$gender, datafile0$cognitive)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.