beginner
beginner

Reputation: 1059

How to save t-test output with names of columns into a dataframe in R?

I have a dataframe df with following data:

df <- structure(list(group = c("cluster2", "cluster2", 
"cluster1", "cluster2", "cluster2", "cluster2", 
"cluster2", "cluster1", "cluster1", "cluster2"
), One = c(-0.614639315096381, 0.88834977627436, 
0.0832368160901144, 0.00321829065579383, -1.04180850739839, 0.0631077979797929, 
0.590396305169489, 0.103255370028437, 0.309521983720179, -0.101131803547097
), two = c(-0.431029772056812, 1.01576532724102, 
1.8217432927475, 0.369890996511319, -1.67807784856458, 0.470436956726829, 
0.0136060165886214, -0.975047520238315, -0.72883862189043, -0.215444548513809
), three = c(0.319549563636166, -0.331820109901466, 
-0.522498726621558, -0.533583057293564, -1.07557447076409, -1.2439536016024, 
0.137327193404068, 0.824807312120951, 0.890772513135045, -0.699907273864071
), four = c(0.0242164205055869, 0.127580710965884, 
0.939940141796124, -0.95328238190511, -1.12539228910431, -0.57463577872874, 
0.0989572749173223, 0.581259982380773, 1.06395223195912, -1.30497643408927
), five = c(0.0672370094329536, 0.46800631599416, 
0.0336342629045394, -1.5260321604671, -0.108501672097744, -0.00598870144824637, 
-0.135182382987021, 0.697107825231465, 0.736189139343644, -0.822125529718815
), six = c(-0.6376725640001, 0.375979360195534, 
0.850685206672859, -1.7888882165231, -1.65086216108027, -0.224594156132765, 
0.106452755431465, 0.431408065353943, 1.02765650619415, -0.459408909488439
)), row.names = c(NA, 10L), class = "data.frame")

I applied t-test on each column comparing "cluster2" vs "cluster1"

tests <- lapply(seq(2, length(df)), function(x){t.test(df[,x]~df[,1])})

t-test results are in a list. So, I'm trying to extract all information into a dataframe.

v1 <- sapply(tests, function(x) x$p.value)
names(v1) < names(tests)

This gave logical(0).

Can anyone please tell how to save all the results into dataframe?

Upvotes: 0

Views: 609

Answers (2)

Karolis Koncevičius
Karolis Koncevičius

Reputation: 9656

If you don't mind using an external package then:

library(matrixTests)
col_t_welch(df[df$group=="cluster2",-1], df[df$group=="cluster1",-1])

      obs.x obs.y obs.tot      mean.x     mean.y  mean.diff     var.x      var.y    stderr       df  statistic       pvalue   conf.low   conf.high alternative mean.null conf.level
One       7     3      10 -0.03035821 0.16533806 -0.1956963 0.4347748 0.01569194 0.2595021 6.906193 -0.7541221 0.4756968552 -0.8110149  0.41962235   two.sided         0       0.95
two       7     3      10 -0.06497898 0.03928572 -0.1042647 0.7347812 2.39802096 0.9509517 2.545136 -0.1096425 0.9207496910 -3.4608429  3.25231355   two.sided         0       0.95
three     7     3      10 -0.48970882 0.39769370 -0.8874025 0.3385390 0.63615343 0.5103076 2.964909 -1.7389561 0.1815091371 -2.5223572  0.74755220   two.sided         0       0.95
four      7     3      10 -0.52964750 0.86171745 -1.3913649 0.3785659 0.06283704 0.2739097 7.963842 -5.0796483 0.0009668828 -2.0235014 -0.75922852   two.sided         0       0.95
five      7     3      10 -0.29465530 0.48897708 -0.7836324 0.4417576 0.15588465 0.3392194 6.575237 -2.3101050 0.0565252579 -1.5963836  0.02911888   two.sided         0       0.95
six       7     3      10 -0.61128484 0.76991659 -1.3812014 0.6884335 0.09377073 0.3600063 7.996676 -3.8366033 0.0049749882 -2.2114376 -0.55096530   two.sided         0       0.95

Upvotes: 1

larenite
larenite

Reputation: 287

You have a syntax error, which is why you're getting logical(0). names(v1) < names(tests) should be names(v1) <- names(tests).

Also, names(tests) is NULL, so that won't be helpful. Assuming you want the names to correspond to the column name used to specify the column in the t-test, you could use:

cols <- colnames(df)[2:ncol(df)]
tests <- lapply(cols, function(x){t.test(df[,x]~df[,1])})
names(v1) <- cols

Then use data.frame(v1) to convert to a data.frame.

Upvotes: 0

Related Questions