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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:13:38+00:00 2026-05-30T21:13:38+00:00

I am attempting to apply a function to a dataset for each combination of

  • 0

I am attempting to apply a function to a dataset for each combination of multiple factors. The function has two arguments. I have attempted solutions based on previous questions on conditional summing in R and using the plyr package with unsuccessful results.

An example is useful. Here, x refers to “events” and y to “responses” for two conditions.

dat <- data.frame(x=c(0,0,1,1,0,0,1,1),
              y=c(2,1,1,2,1,2,1,0),
              g1=c("a","a","a","a","b","b","b","b"),
              g2=c("c","d","c","d","c","d","c","d"))

attach(dat)

I can get counts or sums etc just fine:

numberTrials <- aggregate(y,list(g1,g2),length)
nEvents <- aggregate(x,list(g1,g2),sum)

Now I want to express the number of “2” responses (y==2) to an event (x==1) as a proportion of the total number of events, for each combination of the group factors i.e. length(y[x==1 & y==2])/sum(x).

I’ve tried writing a function to do this calculation, then applying the function to each subset using by:

propFun <- function(events,response){
# where x is the events and y is the response
nEvents <- sum(events)
nResp2ToEvent <- length(response[events==1 & response==2])
propFAs <- nResp2ToEvent/nEvents
return(propFAs)
}

dataProp <- by(dat,list(g1,g2),propFun(events=x),response=y)

However, the call to by produces:

Error in propFun(events = x) : 
argument "response" is missing, with no default

I have been similarly unsuccessful using sapply and ddply.

I’m sure that the error I get has a simple syntactical fix; however I would also be interested in any better solutions to the overall problem. Thanks

  • 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-30T21:13:40+00:00Added an answer on May 30, 2026 at 9:13 pm

    I think this is what you’re after, using ddply and summarise:

    ddply(dat,.(g1,g2),summarise,ev = length(y[x==1 & y==2])/sum(x))
    
      g1 g2 ev
    1  a  c  0
    2  a  d  1
    3  b  c  0
    4  b  d  0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jqgrid with a subgrid. I am attempting to apply different colors
I have an image in OpenGL that I am attempting to apply a simple
I'm attempting to add this .click function to each image I'm sorting with the
I'm attempting to compare two tables of equal length with a function, since I
for days I have been attempting to apply a color to the selected page
I have a UILabel to which I am attempting to apply a semi-transparent gradient
I'm attempting to apply Ayende's order search from here to an existing index. The
When I want to call a function in javascript with arguments supplied from elsewhere
I'm attempting to apply a stylesheet to an XML document using Saxon. Given an
I'm attempting to apply an XSL transform to an XML file inside an SSIS

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.