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 of a time series of data, I would like to
I have a data frame composed of numeric and non-numeric columns. I would like
I have a data set of the form: df <- data.frame(var1 = c(1976-07-04 ,
Another ggplot legend question! I have a dataset of the form test <- data.frame(
I have a data frame like below (20,000 rows by 49 cols). Each row
I have an R data.frame of the form: name masterchef newsnight shameless 1 charliesheen
I have a data.frame that looks like this. x a 1 x b 2
I have a data frame of the form: >df stationid station gear sample lat
I have a data.frame in the form: Date 2011-08-16 2011-08-17 2011-08-28 2011-09-01 2011-09-05 2011-09-06
I have a data frame which looks like this: inten new.probes 12.28280 AFFX-r2-P1-cre-5_at 12.35039

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.