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 8344909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:31:07+00:00 2026-06-09T06:31:07+00:00

I have a trait dataset with one trait per row. I would like to

  • 0

I have a trait dataset with one trait per row. I would like to perform a t.test comparing the means for two specific columns in the dataset, i.e. T_Mean and A_Mean or columns 2 and 8. I have attempted to code this using sapply with no luck, sample dataset and code below. Any help fixing my code is much appreciated!

WW_Summary <- structure(list(Trait =
  c("Morph PC1", "Morph PC2", "Morph PC3", "Morph PC4", "Colour", "Delta15N", "Delta13C"),
  T_Mean = c(-0.000369133942377988, -0.478614374395391, -0.0429785751248986, 0.141563333908087, 5.09447415329768, 7.79253141831239, -20.3678994614004),
  T_SD = c(1.25617540601557, 0.994922039584068, 0.72787412346719, 0.5683273217636, 1.85452769780342, 1.56401940841295, 2.33461396773921),
  T_N = c(615, 615, 615, 615, 561, 557, 557),
  HZ_Mean = c(0.379669406453242, 0.307293731157124, -0.0499328976749929, -0.0563021988086238, 4.74712643678161, 8.4568926056338, -20.8209771126761),
  HZ_SD = c(1.27837645625113, 1.11890593333031, 0.71490087377916, 0.699316698091669, 1.90101932622734, 1.86547215761457, 1.9590774632374),
  HZ_N = c(1137, 1137, 1137, 1137, 1131, 1136, 1136),
  A_Mean = c(-0.818704170327851, -0.104449965981942, 0.157885253051751, -0.0437302662392194, 4.31320754716981, 9.79891783567134, -19.955250501002),
  A_SD = c(1.29535566832773, 0.97478498249366, 0.678515276691309, 0.563663991917263, 1.63029422418466, 2.06376134152221, 1.47077203157055),
  A_N = c(527, 527, 527, 527, 530, 499, 499)),
  .Names = c("Trait", "T_Mean", "T_SD", "T_N", "HZ_Mean", "HZ_SD", "HZ_N", "A_Mean", "A_SD", "A_N"),
  class = "data.frame", row.names = c(NA, -7L))

## Perform t-test separately for each trait (row) comparing means for T_Mean & A_Mean (columns 2 and 8)
WW_Summary_T <- data.frame(t(sapply(WW_Summary[,c(2,8)], function(temp)
                unlist(t.test(temp, alternative = c("two.sided"))[c("statistic",
                "parameter", "p.value", "conf.int")]))))
  • 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-09T06:31:08+00:00Added an answer on June 9, 2026 at 6:31 am

    I think you’re just using the wrong apply family member.

    Can you try this and see if it gives you what you’re looking for?

    apply(WW_Summary[, c(2, 8)], 1, 
          function(temp) unlist(t.test(temp, aslternative = c("two.sided"))
          [c("statistic", "parameter", "p.value", "conf.int")]))
    

    Update

    @dickoa is correct: you’re probably doing the wrong calculation here. Still, the same concept applies:

    data.frame(cbind(WW_Summary[1], 
             t(apply(WW_Summary[, c(2:4, 8:10)], 1, function(temp)
               unlist(
                 tsum.test(mean.x = temp[[1]], s.x = temp[[2]], n.x = temp[[3]],
                           mean.y = temp[[4]], s.y = temp[[5]], n.y = temp[[6]]))
                     [c("statistic.t", "parameters.df", "p.value", 
                        "conf.int1", "conf.int2")]))))
    #       Trait       statistic.t    parameters.df              p.value
    # 1 Morph PC1  10.7920944667109 1102.17477516966 6.99739270551733e-26
    # 2 Morph PC2 -6.40501752763609  1119.8038108643 2.20872274986877e-10
    # 3 Morph PC3  -4.8221965806503 1131.93025335657 1.61345381252079e-06
    # 4 Morph PC4  5.51685228304417 1116.04949237415 4.28798959831121e-08
    # 5    Colour  7.40032254940697 1083.43427031755 2.71950155801888e-13
    # 6  Delta15N -17.6468194524627 923.361537684413 2.79180235004071e-60
    # 7  Delta13C -3.47262865160519 949.662208494884 0.000538633884372937
    #            conf.int1          conf.int2
    # 1  0.669552939095012  0.967117133675934
    # 2  -0.48878427646537 -0.259544540361528
    # 3   -0.2825915783163 -0.119136078036999
    # 4  0.119393147514491  0.251194052780122
    # 5  0.574117940682135  0.988415271573606
    # 6  -2.22952047960588  -1.78325235511202
    # 7 -0.645846712936065 -0.179451207860735
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a sealed trait which have a declared method that
I have a trait and an implementation looking like: trait Foo[A] { def bar[B
I have two traits, each with a type parameter for one of its members.
I have a classification problem where I would like to predict an outcome, but
I would like to shuffle values in an HashMap. I have following type of
Say I have a trait that has two lists. Sometimes I'm interested in the
Let's suppose I have a trait with two type parameters, e.g. trait Qux[A, B]
I have a trait, named Init : package test trait Init { def init():
Suppose I have the following trait with two abstract vals trait Base { val
I have scala trait as follows trait Id { @javax.persistence.Id @GeneratedValue(strategy=GenerationType.IDENTITY) @BeanProperty var id:

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.