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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:29:38+00:00 2026-06-15T18:29:38+00:00

Possible Duplicate: Averaging column values for specific sections of data corresponding to other column

  • 0

Possible Duplicate:
Averaging column values for specific sections of data corresponding to other column values

I would like to analyze a dataset by group. The data is set up like this:

Group   Result   cens
   A    1.3        1
   A    2.4        0
   A    2.1        0
   B    1.2        1
   B    1.7        0
   B    1.9        0

I have a function that calculates the following

sumStats = function(obs, cens) {
detects = obs[cens==0]
nondetects= obs[cens=1]
mean.detects=mean(detects) 
return(mean.detects) }

This of course is a simple function for illustration purpose. Is there a function in R that will allow me to use this home-made function that needs 2 variables input to analyze the data by groups.

I looked into the by function but it seems to take in 1 column data at a time.

  • 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-15T18:29:39+00:00Added an answer on June 15, 2026 at 6:29 pm

    Import your data:

    test <- read.table(header=TRUE,textConnection("Group   Result   cens
       A    1.3        1
       A    2.4        0
       A    2.1        0
       B    1.2        1
       B    1.7        0
       B    1.9        0"))
    

    Though there are many ways to do this, using by specifically you could do something like this (assuming your dataframe is called test):

    by(test,test$Group,function(x) mean(x$Result[x$cens==1]))
    

    which will give you the mean of all the Results values within each group which have cens==1

    Output looks like:

    test$Group: A
    [1] 1.3
    ----------------------------------------------------------------------
    test$Group: B
    [1] 1.2
    

    To help you understand how this might work with your function, consider this:
    If you just ask the by statement to return the contents of each group, you will get:

    > by(test,test$Group,function(x) return(x))
    test$Group: A
      Group Result cens
    1     A    1.3    1
    2     A    2.4    0
    3     A    2.1    0
    ----------------------------------------------------------------------- 
    test$Group: B
      Group Result cens
    4     B    1.2    1
    5     B    1.7    0
    6     B    1.9    0
    

    …which is actually 2 data frames with only the rows for each group, stored as a list:
    This means you can access parts of the data.frames for each group as you would before they they were split up. The x in the above functions is referring to the whole sub-dataframe for each of the groups. I.e. – you can use individual variables as part of x to pass to functions – a basic example:

    > by(test,test$Group,function(x) x$Result)
    test$Group: A
    [1] 1.3 2.4 2.1
    -------------------------------------------------------------------
    test$Group: B
    [1] 1.2 1.7 1.9
    

    Now, to finally get around to answering your specific query!
    If you take an example function which gets the mean of two inputs separately:

    sumStats = function(var1, var2) {
       res1 <- mean(var1)
       res2 <- mean(var2)
       output <- c(res1,res2)
       return(output)
    }
    

    You could call this using by to get the mean of both Result and cens like so:

    > by(test,test$Group,function(x) sumStats(x$Result,x$cens))
    test$Group: A
    [1] 1.9333333 0.3333333
    ---------------------------------------------------------------------- 
    test$Group: B
    [1] 1.6000000 0.3333333
    

    Hope that is helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: C# driver development? I would like to know if I can do
Possible Duplicate: Merging PHP array, one as Keys the other as Values? i have
Possible Duplicate: Difference of create Index by using include column or not using Edit:
Possible Duplicate: Difference Between Equals and == in which cases equals() works exactly like
Possible Duplicate: Arithmetic in ruby The status_update model takes 2 values in, does some
Possible Duplicate: Trying to get tables next to each other horizontal I have two
Possible Duplicate: how to add a reference to the system.data.SQLite.dll to the windows phone
Possible Duplicate: Why do I see a double variable initialized to some value like
Possible Duplicate: Is Java pass-by-reference? if we have big byte[] array (like 40Mb) and

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.