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

The Archive Base Latest Questions

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

I have the following (dummy) data: d <- structure(list(group = structure(c(1L, 1L, 1L, 1L,

  • 0

I have the following (dummy) data:

d <- structure(list(group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 5L, 
5L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 
2L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("apple", "grapefruit", 
"orange", "peach", "pear"), class = "factor"), type = structure(c(2L, 
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 
1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("large", 
"small"), class = "factor"), location = structure(c(1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("P1", 
"P2", "P3"), class = "factor"), diameter = c(17.2, 19.1, 18.5, 
23.3, 22.9, 19.4, 11.1, 11.8, 6.8, 3.2, 7.9, 5.6, 8.4, 9.2, 9.7, 
17.1, 19.4, 18.9, 11.8, 10.6, 10.1, 18.8, 17.9, 13.2, 8.5, 8.9, 
7.2, 10.1, 8.7, 6.6)), .Names = c("group", "type", "location", 
"diameter"), class = "data.frame", row.names = c(NA, -30L))

I’d like to create a new data frame from this, deriving ratios from the “diameter” variable for each level of 3 factors: “location”, “type”, and “group”.

P3.P1.L <- with(d, diameter[group=="pear" & type=="large" & location=="P3"] / diameter[group=="pear" & type=="large" & location=="P1"] )
P2.P1.L <- with(d, diameter[group=="pear" & type=="large" & location=="P2"] / diameter[group=="pear" & type=="large" & location=="P1"] )
P3.P1.S <- with(d, diameter[group=="pear" & type=="small" & location=="P3"] / diameter[group=="pear" & type=="small" & location=="P1"] )
P2.P1.S <- with(d, diameter[group=="pear" & type=="small" & location=="P2"] / diameter[group=="pear" & type=="small" & location=="P1"] )

The final data.frame would look something like this:

group, type, P2.P1, P3.P1
pear, large, 1.75, 2.469
pear, small, 0.613, 1.063
apple, large, ..., ...
apple, small, ..., ...

Obviously, I could do this like i’ve illustrated above – logically indexing the correct levels of the 3 factors in each instance. The problem is, in my real data I have about 40 levels in the “group” factor (though still only 2 in “type”). I’d like a solution that will allow me to use logical indexing with “location” and perhaps “type”, and then iterate through all the levels of “group”. For example, something like:

with(d, by(d, group, function(x) diameter[type=="large" & location=="P3"] / diameter[type=="large" & location=="P1"]) )

But this doesn’t quite do what i’d like (and indexing with “group==x” doesn’t work either).

A solution that keeps track of the association of each ratio with its “group” and “type” factor levels, and then places these into the new data frame, as illustrated in the desired output above, would be spectacular. Any suggestions on how to approach this would be much appreciated.

  • 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-30T09:38:53+00:00Added an answer on May 30, 2026 at 9:38 am

    You can use dcast to convert the data to a wider format.

    library(reshape2)
    d <- dcast( d, group + type ~ location )
    

    It is then straightforward to compute the ratios you want, for instance:

    transform( d, P2.P1=P2/P1, P3.P1=P3/P1 )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following dummy class structure and I am trying to find out
I have the following code: Imports System.Data Imports System.Data.OleDb Partial Class Dummy Inherits System.Web.UI.Page
I have the following data in a CSV file. 2454,dum my 12345,dummy 300001,du m
I have Following dummy table with data: ACID srno date(mm/dd/yyyy) name 3 1 04/12/2010
I have the following dummy code: dt<-data.frame(country=letters[1:20],val=rnorm(20),siz=rnorm(20)) qplot(x=country,y=val,data=dt,geom=point,size=siz) Now I want to increase the
Let's say I have the following content: Lorem Ipsum is simply dummy text of
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have following classes. class A { public: void fun(); } class B: public
I have the following class, where I want to use the method location_change within
I have a table with following columns and data FldID | Rev | Words

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.