I tried to do t-test to all columns (two at a time) of my data frame, and extract only the p-value. Here is what I have come up with:
for (i in c(5:525) ) {
t_test_p.value =sapply( Data[5:525], function(x) t.test(Data[,i],x, na.rm=TRUE)$p.value)
}
My questions are: 1. is there a way to do it without a loop? 2. how to capture the results of the t-test.
To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.
To check for equality of three columns by row, we can use logical comparison of equality with double equal sign (==) and & operator.
Group By Multiple Columns in R using dplyrUse group_by() function in R to group the rows in DataFrame by multiple columns (two or more), to use this function, you have to install dplyr first using install. packages('dplyr') and load it using library(dplyr) . All functions in dplyr package take data.
I would recommend to convert your data frame to long format and use pairwise.t.test
with appropriate p.adjust
:
> library(reshape2)
>
> df <- data.frame(a=runif(100),
+ b=runif(100),
+ c=runif(100)+0.5,
+ d=runif(100)+0.5,
+ e=runif(100)+1,
+ f=runif(100)+1)
>
> d <- melt(df)
Using as id variables
>
> pairwise.t.test(d$value, d$variable, p.adjust = "none")
Pairwise comparisons using t tests with pooled SD
data: d$value and d$variable
a b c d e
b 0.86 - - - -
c <2e-16 <2e-16 - - -
d <2e-16 <2e-16 0.73 - -
e <2e-16 <2e-16 <2e-16 <2e-16 -
f <2e-16 <2e-16 <2e-16 <2e-16 0.63
P value adjustment method: none
> pairwise.t.test(d$value, d$variable, p.adjust = "bon")
Pairwise comparisons using t tests with pooled SD
data: d$value and d$variable
a b c d e
b 1 - - - -
c <2e-16 <2e-16 - - -
d <2e-16 <2e-16 1 - -
e <2e-16 <2e-16 <2e-16 <2e-16 -
f <2e-16 <2e-16 <2e-16 <2e-16 1
P value adjustment method: bonferroni
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With