Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9265315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:02:29+00:00 2026-06-18T14:02:29+00:00

I have data frame that looks like this: set.seed(123) df <- data.frame(factor1 = rep(c(A,

  • 0

I have data frame that looks like this:

set.seed(123)
df <- data.frame(factor1 = rep(c("A", "B"),50),
                 factor2 = rep(c("X","X", "Y", "Y"),25),
                 value = rnorm(100))

I want to calculate some summary values for factor1:factor2 pairs. I calculated the means and sd using:

summary <- as.matrix(cast(df, factor1~factor2, mean))
summary.sd <- as.matrix(cast(df, factor1~factor2, sd))
summary.table <- t(rbind(summary, summary.sd))
colnames(summary.table) <- c("A.mean", "B.mean", "A.sd", "B.sd")

But I would like to add to summary.table the p-value from a t.test comparing A vs B. So far I have done this, but not only does this not write to summary.table but i can’t get the names of the factor2 variable to print out along with it:

for (measurement in levels(df$factor2)) print(t.test(value~factor1, data=subset(df, factor2==measurement)))

I figure there must be some simple way to do this, or maybe a package that I am not aware of that would make this much more straightforward.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:02:30+00:00Added an answer on June 18, 2026 at 2:02 pm

    I’d do it this way:

    First, get mean and sd summaries using ddply from plyr using summarise

    require(plyr)
    require(reshape2)
    o1 <- ddply(df, .(factor1, factor2), summarise, mean = mean(value), sd=sd(value))
    
    #   factor1 factor2       mean        sd
    # 1       A       X 0.03746854 0.8730525
    # 2       A       Y 0.18352432 0.7635439
    # 3       B       X 0.10317706 1.0494930
    # 4       B       Y 0.03745372 0.9876173
    

    Then, get p-values for t-test with NULL mean(A) = mean(B) for both X and Y levels in factor2:

    o2 <- ddply(df, .(factor2), summarise, pval=t.test(value ~ factor1)$p.value)
    
    #   factor2      pval
    # 1       X 0.8108754
    # 2       Y 0.5614256
    

    Then, using reshape2‘s melt and dcast cast o1 to the desired format.

    o1.mc <- dcast(melt(o1, c("factor1", "factor2")), factor2 ~ variable + factor1)
    
    #   factor2     mean_A     mean_B      sd_A      sd_B
    # 1       X 0.03746854 0.10317706 0.8730525 1.0494930
    # 2       Y 0.18352432 0.03745372 0.7635439 0.9876173
    

    Now, merge it with o2:

    merge(o1.mc, o2)
    
    #   factor2     mean_A     mean_B      sd_A      sd_B      pval
    # 1       X 0.03746854 0.10317706 0.8730525 1.0494930 0.8108754
    # 2       Y 0.18352432 0.03745372 0.7635439 0.9876173 0.5614256
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that we have a data frame that looks like set.seed(7302012) county <- rep(letters[1:4],
I have a set of data that looks like this, species<-ABC ind<-rep(1:4,each=24) hour<-rep(seq(0,23,by=1),4) depth<-runif(length(ind),1,50)
I have a dataset that looks a little like this: a <- data.frame(x=rep(c(1,2,3,5,7,10,15,20), 5),
I have a data set that looks like this: ByYear <- data.frame( V1 =
I have a R data frame that looks like this: Country Property Value Canada
Assuming I have a melted data.frame that looks like this: variable value 1 A
I have a data frame that looks like this: variable=c(alpha,beta1,beta2) value=c(22,11,33) df=data.frame(variable=variable, value=value) df
I have a data.frame that looks like this: > head(ff.df) .id pio caremgmt prev
I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.