variables have different lengths

Hi everyone!
I have two variables (v_10_2_24 and v_10_2_24).

I need to make a correlation between those to but first i am asked to eliminate unwanted data from both of them before using "cor"
This is how it looks:
valoracionv<-subset(colmun,subset=v_10_2_24!=997&v_10_2_24!=998&v_10_2_24!=999)

rol<-subset(colmun,subset=v_10_4_3!="ns")
rol2<-subset(rol,subset=v_10_4_3!="nc")

The problem is that when I try to operate them or to correlate them there is a mistake which says that both variables have different lengths, so there is an error which says that the length of the variables is different so the cannot be correlated.

also, when i try to correlate them now, there is another mistake which says that my variable need to be changed to numeric but I really need it as a factor.

If anyone could help me I'll be very thankful

Welcome.

We can't see your dataset because we don't have it. You can provide a reproducible example though.

both variables have different lengths

You'll need to ensure that they are of the same length.

variable need to be changed to numeric but I really need it as a factor

You can't correlate a factor variable.

1 Like

I think Spearman correlation rather than Pearson might be used.

2 Likes

Thank you for your answer William!

I already checked what you advised me and is already corrected. The issue now is that another error came up. R is reading my variables as atomic vector so when I need to operate them, and use the $ symbol, it says it is impossible: I checked online and I used the data frame alternative but the response when I print the variables is NULL, therefore, I can not effectuate a regression with them.
Do you know how cain I solve this issue?

Here are the commands:

valoracionv<-subset(colmun,subset=v_10_2_24!=997&v_10_2_24!=998&v_10_2_24!=999)
valoracionv$v_10_2_24
valoracionv<-as.numeric(valoracionv$v_10_2_24)

valoracionv<-as.data.frame(t(valoracionv))
valoracionv$v_10_2_24
NULL
rol<-subset(colmun,subset=v_10_4_3!="ns"&v_10_4_3!="nc")
rol<-as.data.frame(t(rol$v_10_4_3))
rol$v_10_4_3
NULL

Can you provide a reproducible example?

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.