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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:11:22+00:00 2026-05-26T12:11:22+00:00

I have a data frame of the form: Family Code Length Type 1 A

  • 0

I have a data frame of the form:

  Family Code Length Type
1      A    1     11 Alpha
2      A    3      8 Beta
3      A    3      9 Beta
4      B    4      7 Alpha
5      B    5      8 Alpha
6      C    6      2 Beta
7      C    6      5 Beta
8      C    6      4 Beta

I would like to reduce the data set to one containing unique values of Code by taking a mean of Length values, but to retain all string variables too, i.e.

  Family Code Length Type
1      A    1     11 Alpha
2      A    3    8.5 Beta
3      B    4      7 Alpha
5      B    5      8 Alpha
6      C    6   3.67 Beta

I’ve tried aggregate() and ddply() but these seem to replace strings with NA and I’m struggling to find a way round this.

  • 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-26T12:11:22+00:00Added an answer on May 26, 2026 at 12:11 pm

    Since Family and Type are constant within a Code group, you can “group” on those as well without changing anything when you use ddply. If your original data set was dat

    ddply(dat, .(Family, Code, Type), summarize, Length=mean(Length))
    

    gives

      Family Code  Type    Length
    1      A    1 Alpha 11.000000
    2      A    3  Beta  8.500000
    3      B    4 Alpha  7.000000
    4      B    5 Alpha  8.000000
    5      C    6  Beta  3.666667
    

    If Family and Type are not constant within a Code group, then you would need to define how to summarize/aggregate those values. In this example, I just take the single unique value:

    ddply(dat, .(Code), summarize, Family=unique(Family), 
      Length=mean(Length), Type=unique(Type))
    

    Update

    Similar options using dplyr are

     library(dplyr)
     dat %>% 
         group_by(Family, Code, Type) %>%
         summarise(Length=mean(Length))
    

    and

      dat %>%
         group_by(Code) %>%
         summarise(Family=unique(Family), Length=mean(Length), Type=unique(Type))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame where one particular column has a set of specific
I have a data frame with gaps like this: Var1 Var2 Var3 1 NA
I have a data.frame from this code: my_df = data.frame(read_time = c(2010-02-15, 2010-02-15, 2010-02-16,
I have a data.frame in R that looks like this: score rms template aln_id
I have an R data.frame of the form: name masterchef newsnight shameless 1 charliesheen
i have a data frame like this A B value 1 1 0.123 2
I have a data.frame that looks like this. x a 1 x b 2
I have data frame with some numerical variables and some categorical factor variables. The
I have a data.frame with 2 columns: Node A, Node B. Each entry in
I have a data frame with two columns. First column contains categories such as

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.