Row-wise correlation

Hi everybody,

I have a large data table containing 2 sets (X,Y) of 4 paired and temporally connected observations (2017-2021) by actors, the first few lines of which are as below. I generally intend to calculate row-wise correlations between the 2 sets by the method of spearman.

data.frame(
stringsAsFactors = FALSE,
Actor = c("A", "B", "C", "D"),
X2017 = c(1066500, 100464, 77618, 349779),
X2018 = c(2093021, 189849, 43380, 322751),
X2019 = c(1792100, 238745, 30213, 462867),
X2020 = c(2404909, 291408, 29635, 525696),
X2021 = c(2421116, 402740, 23253, 622501),
Y2017 = c(26, 60, 12, 31),
Y2018 = c(28, 58, 12, 31),
Y2019 = c(29, 48, 12, 31),
Y2020 = c(29, 49, 12, 31),
Y2021 = c(29, 49, 12, 31)
)

I was able to find 2 corresponding and presumably suitable codes:

(1)
l <- apply(X = df, MARGIN = 1, FUN = function(x) cor.test(x[2:6], x[7:11]))
lapply(X = l, FUN = function(x) x$estimate)

(2)
cor = apply(df, MARGIN = 1, FUN = function(x) return(cor.test(x[2:6], x[7:11])$estimate))

Unfortunately, both return the error "x" must be numeric desite opposing assingnments.
Does anybody have an advice regarding this issue and/or the general coding?

Thank you so much in advance!

when you are using apply you are implicitly relying on R's matrix/array functionality. these require all data to be of consistent type. R's rules of coercion to decide what to do : Data structures · Advanced R..

I..e in your data the Actor fields that you dont use, have the undesired effect of having all the data be coerced to character.
Therefore you can exclude that column

(1)
l <- apply(X = df, MARGIN = 1, FUN = function(x) cor.test(x[2:6], x[7:11]))
lapply(X = l, FUN = function(x) x$estimate)

becomes

l <- apply(X = df[,-1], MARGIN = 1, FUN = function(x) cor.test(x[2:6], x[7:11]))
lapply(X = l, FUN = function(x) x$estimate)

the critical part being df[,-1]

1 Like

Ouh, I totally missed that! Thank you so much for your immediate help!

This topic was automatically closed 7 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.