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

  • Home
  • SEARCH
  • 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 8467085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:32:23+00:00 2026-06-10T15:32:23+00:00

I am trying to calculate some simple ratios and use R’s bracket notation to

  • 0

I am trying to calculate some simple ratios and use R’s bracket notation to denote the baseline for the ratio.

Now I am struggling with defining a function that lets me parameterize the baseline.
I don’t want to hardcode it, since I have a couple of those. And, I really don’t understand what R is exactly doing and am very curious how to achieve the desired behavior.

Here some code based on example data:

data("singer", package = "lattice")

# this is what I want, but what currently doesn't work
my_ratio <- function(voice) {
  ddply(singer, ~ voice.part,
        transform,
        # how do I refer to the voice variable here?
        # it looks like it misunderstands it as column?
        ratio = height / mean(height[voice.part == voice]))
}

# this version works with a hardcoded voice part
my_ratio_hard <- function() {
  ddply(singer, ~ voice.part,
        transform,
        ratio = height / mean(height[voice.part == "Soprano 1"]))
}
  • 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-10T15:32:24+00:00Added an answer on June 10, 2026 at 3:32 pm

    How about this:

    my_ratio <- function(voice) {
      my_transform <- function(x) {
        transform(x, ratio = height / mean(height[voice.part == voice]))
      }
      ddply(singer, ~voice.part, my_transform)
    }
    

    Did you really only want to scale the rows matching voice and have NaN elsewhere (that’s also what your hard-coded function does)?

    A more compact version of the above:

    my_ratio <- function(voice) {
      ddply(singer, ~voice.part, 
        function(x) transform(x, ratio = height/mean(height[voice.part == voice])))
    }
    

    and if you actually want to scale all the records (as your comment suggests):

    my_ratio <- function(voice) {
      scale <- with(singer, mean(height[voice.part == voice]))
      ddply(singer, ~voice.part, 
        function(x) transform(x, ratio = height / scale))
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the RQuantLib package to calculate greeks for some options but
I am trying to calculate my frustum to do some simple bounding box tests.
I am trying to conditionally calculate some values based on user inputs in a
I am trying to write some code to calculate dividend yields. I need to
I'm trying to write a simple tracking routine to track some points on a
I'm trying to learn object oriented PHP by porting some of the simple exercises
I'm trying to generate some sql that is used to calculate some final scores
I'm trying to write a simple game/utility to calculate poker odds. I know there's
I am trying to perform a simple calculation to some data returned from SQL
Am trying to calculate the number of rows in a table depending on a

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.