1

It is possible to calculate the correlation coefficients and p-values in groups? For example with this dataset:

df<-read.csv("http://renatabrandt.github.io/EBC2015/data/varechem.csv", row.names=1)

df

It is possible to calculate between the first 3 columns N, P, K, the correlation between the next three columns of Ca, Mg and S and so on?

I couldn't find any arguments in the cor function that would allow me to specify that. I'm no expert, but I suspect I could split my dataset into sub-datasets first. But even then I don't know how it can be implied that in the cor function.

Anyone have an idea how I could go about this, maybe another function? Or do I have to go the long way and just do each correlation separately? My real data set is relatively large, so an automated solution would help me a lot!

4
  • The second set is Ca, Mg, S. So i want to calculate between N-P, N-K, P-K and Ca-Mg, Ca-S, Mg-S and so on. Commented Mar 15, 2023 at 16:43
  • When you say sets of 3, are you doing correlation between 1 to 3 and 4 to 6, then 7 to 9 and 10 to 12 and 13 to 15 and 16 to 19?? Commented Mar 15, 2023 at 16:47
  • Exactly, up to the group of columns 88 to 90 Commented Mar 15, 2023 at 16:57
  • then, the update should work for you Commented Mar 15, 2023 at 16:58

1 Answer 1

1

Perhaps this helps

i1 <- rep(c(TRUE, FALSE), each = 3)
cor(df[i1], df[!i1])
                Ca          Mg           S          Zn          Mo    Baresoil
N        -0.27089189 -0.16377388 -0.26225073 -0.13216191 -0.05773585  0.10592277
P         0.73720757  0.59793864  0.75272609  0.70231212  0.17246511  0.01389203
K         0.66482924  0.62775899  0.84377126  0.60004281  0.06819748  0.16855851
Al       -0.20586385 -0.11839842  0.35975081 -0.05510622  0.51031000 -0.39955068
Fe       -0.33216455 -0.20221718  0.05651145 -0.31205825  0.22059378 -0.45746292
Mn        0.44331615  0.25758427  0.27457837  0.36446210 -0.20497945  0.24617616
Humdepth  0.24364456  0.37140078  0.15821813  0.13964926  0.05860733  0.59244384
pH        0.09142358 -0.09252275 -0.18689505 -0.08692419 -0.17496367 -0.53181541
> 

Or may be

lst1 <- split.default(df, as.integer(gl(ncol(df), 6, ncol(df))))
> lapply(Filter(\(x) ncol(x)  == 6, lst1), \(x) cor(x[1:3], x[4:6]))
$`1`
          Ca         Mg          S
N -0.2708919 -0.1637739 -0.2622507
P  0.7372076  0.5979386  0.7527261
K  0.6648292  0.6277590  0.8437713

$`2`
            Zn         Mo   Baresoil
Al -0.05510622  0.5103100 -0.3995507
Fe -0.31205825  0.2205938 -0.4574629
Mn  0.36446210 -0.2049795  0.2461762

Or could be

lapply(Filter(\(x) ncol(x)  == 6, lst1), \(x) list(cor(x[1:3]), cor(x[4:6])))

-output

$`1`
$`1`[[1]]
           N          P          K
N  1.0000000 -0.2511603 -0.1466368
P -0.2511603  1.0000000  0.7540753
K -0.1466368  0.7540753  1.0000000

$`1`[[2]]
          Ca        Mg         S
Ca 1.0000000 0.7982771 0.5395393
Mg 0.7982771 1.0000000 0.6504151
S  0.5395393 0.6504151 1.0000000


$`2`
$`2`[[1]]
           Al         Fe         Mn
Al  1.0000000  0.8242146 -0.4704864
Fe  0.8242146  1.0000000 -0.4360448
Mn -0.4704864 -0.4360448  1.0000000

$`2`[[2]]
                 Zn         Mo   Baresoil
Zn       1.00000000 0.28200265 0.04450428
Mo       0.28200265 1.00000000 0.03124277
Baresoil 0.04450428 0.03124277 1.00000000
Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, it only outputs a matrix with NAs and the specific columns that I want to examine for correlation are not being examined at the moment :D
@Bellis with your data, I don't get NAs (updated with output)
Ah ok, now it works, thanks. And I probably expressed myself misleadingly, I don't want the correlations between e.g. Ca and N because they are from different groups. I'm looking for the within group correlation, e.g. N and P. I'm very sorry and thank you very much for your effort!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.