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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:32:44+00:00 2026-05-23T14:32:44+00:00

Here is a sample: > tmp label value1 value2 1 aa_x_x xx xx 2

  • 0

Here is a sample:

> tmp
    label   value1  value2
1   aa_x_x  xx      xx
2   bc_x_x  xx      xx
3   aa_x_x  xx      xx
4   bc_x_x  xx      xx

How to calculate median of all repeated labels (or more, of the corresponding values in other data frame columns), but taking into account only the first two letters (ie. “aa_1_1” and “aa_s_3” are the same values)? The list of labels is finite and usable.

I have read about aggregate, %in%, subset and substr, but I am unable to compile anything useful and simple.

Here is what I hope to get:

> tmp.result
    label   median1 some.calculation2
1   aa      xx      xx
2   bc      xx      xx
3   aa      xx      xx
4   bc      xx      xx

Thank you very much.

  • 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-23T14:32:45+00:00Added an answer on May 23, 2026 at 2:32 pm

    Have you tried making a new data frame–I’ll call it tmp2–where tmp2$label==substr(tmp$label,0,2)? From there, you can, for example, use tapply(tmp2$value1,tmp2$label,mean) to get the average values of value1 aggregated over tmp2$label.

    An option using dplyr

    library(dplyr)
    tmp %>%
       group_by(label=sub('_.*$', '', label)) %>% 
       transmute(median1=median(value1), mean1=mean(value2))
    

    Or data.table

     library(data.table)
     setDT(tmp)[,  c('median1', 'mean1') := list(median(value1), 
        mean1= mean(value2)) , .(label=sub('_.*$', '', label))][, c(1,4:5), 
           with=FALSE]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to reuse Apple's Speak Here sample code in my own iPhone
Here is my sample code: from xml.dom.minidom import * def make_xml(): doc = Document()
Here is a sample from Kernighan & Ritchie's The C Programming Language: int getline(char
Here is a sample code to retrieve data from a database using the yield
Here is the sample: Dim TestString As String = Hello, & Chr(0) & World
Here is my sample code. It is meant to be an iterative procedure for
here is some sample javascript: SomeObjectType = function() { } SomeObjectType.prototype = { field1:
Here is a sample code: public class TestIO{ public static void main(String[] str){ TestIO
Here's an sample code, where only the string object is released. NSString *nameOfFile =
Here's the sample code: public static void col (int n) { if (n %

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.