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

  • Home
  • SEARCH
  • 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 4055426
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:41:59+00:00 2026-05-20T14:41:59+00:00

I have a data set that I need to calculate the trait average of

  • 0

I have a data set that I need to calculate the trait average of each unique dam and this is an example data set:

pig <- c(20111,20112,20113,20571,20572,20573,20584,20585,20586)
sex <- c(1,1,2,2,2,2,2,1,2)
wt <- c(1.54,0.84,0.83,1.14,1.42,1.07,1.25,1.05,1.42)
dam <- c(1661,1661,1661,1027,1027,1027,10331,10331,10331)
res <-c(1,1,1,1,1,NA,1,2,1)
mt <- c(2,2,2,1,1,NA,1,1,1)
ms <- c(1,1,1,1,1,NA,1,1,1)
cr <- c(26,24,21,23,25,24,22,22,22)
rt <- c(38.7,37.2,37.8,38.1,38.4,NA,38.1,38,38.3)
data <- data.frame(pig,sex,wt,dam,res,mt,ms,cr,rt)
data

I would like to have something like this:

udam <- c(1661,1027,10331)
tpig <- c(3,3,3)
asex <- c(1.3,2,1.7)
awt <- c(1.07,1.21,1.24)
ares <- c(1,1,1.33)
amt <- c(2,1,1)
ams <- c(1,1,1)
acr <- c(23.7,24,22)
art <- c(37.6,38.25,38.1)
data2 <- data.frame(udam,tpig,asex,awt,ares,amt,ams,acr,art)

I hope that my question is clear enough and any help would be appreciated!

Poasa

  • 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-05-20T14:42:00+00:00Added an answer on May 20, 2026 at 2:42 pm

    The plyr package has very usefull functions to do this. For example, we can use ddply to split a dataframe by a factor, apply a function, and return the results again in a dataframe:

        library('plyr')
    
        ddply(data,.(dam),mean,na.rm=T)
    
        pig      sex   wt   dam      res mt ms       cr       rt
    1 20572 2.000000 1.21  1027 1.000000  1  1 24.00000 38.25000
    2 20112 1.333333 1.07  1661 1.000000  2  1 23.66667 37.90000
    3 20585 1.666667 1.24 10331 1.333333  1  1 22.00000 38.13333
    

    Or use the summarize function for a bit more control:

    ddply(data,.(dam),summarize,
      tpig = 3,
      asex = mean(sex,na.rm=T),
      sexRatio = sum(sex==1)/sum(sex==2),
      awt = mean(wt,na.rm=T),
      ares = mean(res,na.rm=T),
      amt = mean(mt,na.rm=T),
      ams = mean(ms,na.rm=T),
      acr = mean(cr,na.rm=T),
      art = mean(rt,na.rm=T),
      count = length(pig))
        dam tpig     asex sexRatio  awt     ares amt ams      acr      art count
    1  1027    3 2.000000      0.0 1.21 1.000000   1   1 24.00000 38.25000     3
    2  1661    3 1.333333      2.0 1.07 1.000000   2   1 23.66667 37.90000     3
    3 10331    3 1.666667      0.5 1.24 1.333333   1   1 22.00000 38.13333     3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set a data that I need to calculate their consecutive mean
I have a set of 'dynamic data' that I need to bind to the
I have a data set that resembles this: id product_id size color price created_date
I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have a set of data protected by 16bit checksums that I need to
I have a data set that is around 700 rows with eight columns of
I have a data set that that I would like to call in a
I have the following data set that I am trying to plot with ggplot2,
I have a large data set that I'm working with in excel. About 1000+
I have a very large possible data set that I am trying to visualize

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.