Borexino
Borexino

Reputation: 842

How to create dataframe using results (output) of pairwise.t.test

My df:

df <- data.frame(
    measurement = runif(120, min=1, max=25),    
    group = rep(c("A","B","C","D"), each=3, times=10)
)

performing t.test and putting results on new df

ttest_results <- data.frame(pairwise.t.test(df$measurement, df$group))

I get this error:

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class ""pairwise.htest"" to a data.frame

I found this solution that works, but i honestly want to solve this through R

write.table(test[["p.value"]], file="output.csv", sep=",") 

Upvotes: 3

Views: 9990

Answers (1)

maycca
maycca

Reputation: 4090

Following comment of @user3710546, here is how you can get p-values output for pairwise comparisons:

# Create dummy dataframe
df <- data.frame(
  measurement = runif(120, min=1, max=25),    
  group = rep(c("A","B","C","D"), each=3, times=10)
)

# Run  your test and save it to its own object
ttest_results <- pairwise.t.test(df$measurement, df$group)

# Get p-values in a dataframe
ttest_results$p.value

While adding the $ symbol in RStudio, you can notice that p-values area actually stored in a table.

enter image description here

Which results :

  A         B  C
B 1        NA NA
C 1 0.3999325 NA
D 1 1.0000000  1

Now you can nicely display your p-values and check the pair-wise comparisons:

# Convert the data to a table
m<-as.table(p.vals)


# Plot p-values
# ----------------------
library("gplots")

windows(3,3)
balloonplot(t(m), 
            main ="p.values", 
            xlab ="", 
            ylab="",
            label = FALSE, 
            show.margins = FALSE)

enter image description here

Upvotes: 1

Related Questions