Reputation: 119
I do have the following dataframe:
df <- data.frame(
Assay = c("Gene1", "Gene2", "Gene3"),
DT1 = c(1,2,3),
DT2 = c(4,5,6),
DT3 = c(4,5,6),
DT4 = c(0,8,7),
DT5 = c(-1,2,5),
DT6 = c(4,5,3),
DT7 = c(5,2,9),
DT8 = c(0,0,4),
DT9 = c(3,6,2),
DT10 = c(5,9,1),
DT11 = c(2,3,4),
DT12 = c(8,1,6)
)
And I would like to create a column that will contain p-values for groups compared row by row. First 5 columns (2:6) versus Next 7 columns (7:13)
# Perform t-tests row-wise and obtain p-values
p_values <- apply(df[, 2:6], 1, function(row) {
t_test_result <- t.test(row, df[, 7:13])
t_test_result$p.value
})
# Add the p-values column to the dataframe
df$p_values <- p_values
df
For the first row when I use this script I have a p-value of 0.09335115
while if I do it manually:
t.test(c(1,4,4,0,-1),
c(4,5,0,3,5,2,8))
I do have a p-value of 0.1425
What's the issue?
Upvotes: 0
Views: 64
Reputation: 79238
just do:
p_values <- apply(df[-1], 1, function(row) {
t_test_result <- t.test(row[1:5], row[6:12])
t_test_result$p.value
})
p_values
[1] 0.1425172 0.6840726 0.3266262
Upvotes: 2