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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:20:23+00:00 2026-06-19T04:20:23+00:00

I am trying to obtain proportions within subsets of a data frame. For example,

  • 0

I am trying to obtain proportions within subsets of a data frame. For example, in this made-up data frame:

DF<-data.frame(category1=rep(c("A","B"),each=9),
    category2=rep(rep(LETTERS[24:26],each=3),2),
     animal=rep(c("dog","cat","mouse"),6),number=sample(18))

I would like like to calculate the proportion of each of the three animals for each category1 by category2 combination (e.g., out of all animals that are both “A” and “X”, what proportion are dogs?). With prop.table on column 4 of the data frame I can get the proportion that each row makes up of the total “number” column, but I have not found a way to do this for subsets based on category 1 and 2. I also tried splitting the data by category1 and category2 using this:

splitDF<-split(DF,list(DF$category1,DF$category2))

And I was hoping I could then apply a function with prop.table to get the proportions of each animal within each split group, but I cannot get prop.table working because I can’t seem to specify which column of data to apply the function to within the split groups. Does anyone have any tips? Maybe this is possible with plyr or something similar? I can’t find anything in the help forums about ways to get proportions within subsets of data.

  • 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-19T04:20:24+00:00Added an answer on June 19, 2026 at 4:20 am

    You can use function ddply() from library plyr to calculate proportions for each combination and then add new column to data frame.

     library(plyr)     
     DF<-ddply(DF,.(category1,category2),transform,prop=number/sum(number))
     DF
       category1 category2 animal number       prop
    1          A         X    dog     17 0.44736842
    2          A         X    cat      3 0.07894737
    3          A         X  mouse     18 0.47368421
    4          A         Y    dog      2 0.14285714
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to obtain the data contained within the graph on this website .
I am trying to obtain the value part of this syntax: [name:value] I got
I am trying to obtain something similar to the share this on... menu here
I am trying to obtain counts of each combination of levels of two variables,
I am trying to obtain the document inside a frame. The following does not
I'm trying to obtain the Oracle package name used for a Crystal report data
Im trying to obtain javascript .js files from a website using beautifulsoup. This is
I'm trying to obtain data using regular expressions from a html file, by implementing
I'm trying to obtain credentials from ning network using Coldfusion 9, so first this
I'm trying to obtain the correct unicode characters represented by this string: string originalString

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.