Correlation coefficent of one row in R

Viewed 55

I am starting to do statistics and for my project I want to find correlation coefficients in order to find the variable, which correlates best with time. What I would like to do is to find the correlation between ONE row (e.g "A"), which represents a Variable and (or over) time (Columns 1 to 10). So how all the values in row 1 correlate to a timecourse of over lets say 10 days. If I use wrong terminology please excuse me, still a beginner.

# Dataframe
DF <- rbind(sample(1:100,10)/100, sample(1:100,10)/100, sample(1:100,1)/100)
colnames(DF) <- (1:10)
rownames(DF) <- c("A", "B", "C")

If I would do it in excel it would be something like =CORREL(B2:G2, $B$1:$G$1), whereas $B$1:$G$1 would be time and B2:G2 the values. I hope its clear what I try to say.

Thanks for your help

2 Answers

transpose the datset to a long format, and then run the cor() function against the dataset

DF2 <- t(DF)


cor(DF2)

           A          B  C
A  1.0000000 -0.3752275 NA
B -0.3752275  1.0000000 NA
C         NA         NA  1

You can simply pass the cor(DF) function. If you have non numeric column then just simply exclude them.

Related