user3224522
user3224522

Reputation: 1149

Transform corr.test output as a data.frame in R

I am trying to perform a corr.test on my variables using a sliding window:

library("gtools")
library("psych")
pos <- subs$variable
result <- running(subs[,2], subs[,3], fun=corr.test,  width=4, by=1, method="pearson", use = "pairwise.complete.obs", adjust ="BH")
n <-length(pos)
intervals <- paste0(pos[seq(1, n, by=4)],":", pos[seq(4, n, by=1)])
names(result) <- intervals

This is my input file:

subs <- structure(list(variable = c(92, 101, 106, 107, 120, 132, 136, 
140, 146, 147), covmean = c(11355.658, 11450.079, 11479.711, 
11495.132, 11612.053, 11580.158, 11421.684, 11288.105, 11278, 
11239.763), complex = c(1.3717151, 1.3828546, 1.3880071, 1.3879077, 
1.3959816, 1.3830276, 1.3724465, 1.363247, 1.337794, 1.3334625
)), row.names = c(56L, 62L, 65L, 67L, 77L, 82L, 87L, 95L, 97L, 
114L), class = "data.frame")

This is giving me an output as a list, which I do not understand how to transform in a data.frame in order to read the corr.test values for positions identified in intervals.

Upvotes: 1

Views: 333

Answers (2)

StupidWolf
StupidWolf

Reputation: 46888

It is kind of a odd list embedded in a matrix. You don't need the last row, so for one column you can do coerce it into a data.frame by excluding the last row which is a function. We do it for the first column and you can see the result :

as.data.frame(result[1:10,1])
              r n        t           p         se sef adjust   sym ci.lower
NA-NA 0.9938413 4 12.68363 0.006158665 0.07835624   1     BH FALSE 0.730601
           ci.r  ci.upper        ci.p ci.adj.lower.adj  ci.adj.r
NA-NA 0.9938413 0.9998774 0.006158665         0.730601 0.9938413
      ci.adj.upper.adj
NA-NA        0.9998774

Then we iterate over all columns using lapply(1:ncol...), the result is a list of data frames, which we can concatenate with do.call(rbind,..) :

tab = do.call(rbind,
lapply(1:ncol(result),function(i)as.data.frame(result[1:10,i]))
)

intervals = colnames(result)
tab = data.frame(intervals,tab)
rownames(tab) = 1:nrow(tab)

  intervals         r n          t           p         se sef adjust   sym
1       1:4 0.9938413 4 12.6836275 0.006158665 0.07835624   1     BH FALSE
2       2:5 0.9739082 4  6.0690001 0.026091844 0.16047259   1     BH FALSE
3       3:6 0.3609314 4  0.5473282 0.639068593 0.65944239   1     BH FALSE
4       4:7 0.8114000 4  1.9632335 0.188600011 0.41329775   1     BH FALSE
5       5:8 0.9528598 4  4.4413295 0.047140152 0.21454383   1     BH FALSE
6       6:9 0.8405379 4  2.1941742 0.159462136 0.38307708   1     BH FALSE
     ci.lower      ci.r  ci.upper        ci.p ci.adj.lower.adj  ci.adj.r
1  0.73060099 0.9938413 0.9998774 0.006158665       0.73060099 0.9938413
2  0.20036230 0.9739082 0.9994756 0.026091844       0.20036230 0.9739082
3 -0.91891484 0.3609314 0.9815367 0.639068593      -0.91891484 0.3609314
4 -0.67985832 0.8114000 0.9958766 0.188600011      -0.67985832 0.8114000
5 -0.09768892 0.9528598 0.9990425 0.047140152      -0.09768892 0.9528598
6 -0.62730536 0.8405379 0.9965676 0.159462136      -0.62730536 0.8405379
  ci.adj.upper.adj
1        0.9998774
2        0.9994756
3        0.9815367
4        0.9958766
5        0.9990425
6        0.9965676

The result is a data.frame which you can add the corresponding names which I did above with intervals = .

Another option is to use simplify=FALSE to keep it in a list, assign the names and you can the same result:

result = running(subs[,2], subs[,3], fun=corr.test,  
width=4, by=1, method="pearson", 
use = "pairwise.complete.obs", adjust ="BH",simplify=FALSE)

names(result) = interval

tab = do.call(rbind,lapply(result,function(i)data.frame(i[1:10])))

Upvotes: 1

TarJae
TarJae

Reputation: 78917

Do you mean this solution?

df <- structure(list(variable = c(92, 101, 106, 107, 120, 132, 136, 
140, 146, 147), covmean = c(11355.658, 11450.079, 11479.711, 
11495.132, 11612.053, 11580.158, 11421.684, 11288.105, 11278, 
11239.763), complex = c(1.3717151, 1.3828546, 1.3880071, 1.3879077, 
1.3959816, 1.3830276, 1.3724465, 1.363247, 1.337794, 1.3334625
)), row.names = c(56L, 62L, 65L, 67L, 77L, 82L, 87L, 95L, 97L, 
114L), class = "data.frame")

class(df)

Upvotes: 0

Related Questions