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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:29:03+00:00 2026-06-14T11:29:03+00:00

I am having difficulty executing a calculation that is defined iteratively. The following data

  • 0

I am having difficulty executing a calculation that is defined iteratively. The following data serves as an example (actual data set much larger):

## DATA ##
# Columns
   Individual<-c("A","B","C","D","E","F","G","H1","H2","H3","H4","H5","K1","K2","K3","K4","K5")
   P1<-c(0,0,"A",0,"C","C",0, rep("E",5),"H1","H2","H3","H4","H5")
   P2<-c(0,0,"B",0,"D", "E",0,rep("G",5),"H1","H2","H3","H4","H5")
# Dataframe
   myd<-data.frame(Individual,P1,P2,stringsAsFactors=FALSE)


   Individual P1 P2
1           A  0  0
2           B  0  0
3           C  A  B
4           D  0  0
5           E  C  D
6           F  C  E
7           G  0  0
8          H1  E  G
9          H2  E  G
10         H3  E  G
11         H4  E  G
12         H5  E  G
13         K1 H1 H1
14         K2 H2 H2
15         K3 H3 H3
16         K4 H4 H4
17         K5 H5 H5

The data represents the relationship between and Individual and the two parents, P1, P2.

The calculation required, labeled relationA, represents how much each individual is related to A.

By definition, the relationship between A and A is given a value of 1. The values for all other individuals need to be computed based on the information in the table, as follows:

The value of relationA for an individual should be equal to 
   1/2 (the value of relationA of P1 of the individual)  
 + 1/2 (the value of relationA of P2 of the individual)

FOR EXAMPLE

  Individual P1 P2      relationA
1           A  0  0       1
2           B  0  0       0
3           C  A  B       (A = 1 + B = 0)/2 = 0.5
4           D  0  0       0
5           E  C  D       (C= 0.5 + D = 0)/2 = 0.25
6           F  C  E       (C = 0.5 + E = 0.25)/2 = 0.375  

The expected output is as the following:

 Individual P1 P2  relationA
1           A  0  0   1
2           B  0  0   0
3           C  A  B   0.5
4           D  0  0   0
5           E  C  D   0.25
6           F  C  E   0.375
7           G  0  0   0 
8          H1  E  G   0.125
9          H2  E  G   0.125
10         H3  E  G   0.125
11         H4  E  G   0.125
12         H5  E  G   0.125
13         K1 H1 H1   0.125
14         K2 H2 H2   0.125
15         K3 H3 H3   0.125
16         K4 H4 H4   0.125
17         K5 H5 H5   0.125

My difficulty is in expressing this in a proper manner in R. Any help would be 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-06-14T11:29:04+00:00Added an answer on June 14, 2026 at 11:29 am

    Edit:

    more succinctly, you could use sapply and rowSums to do away with the for-loop into a single line of code:

    # Initialize values of relationA
    myd$relationA <- 0
    myd$relationA[myd$Individual=="A"] <- 1
    
    # Calculate relationA
    myd$relationA <-   myd$relationA + rowSums(sapply(myd$Individual, function(indiv) 
         myd$relationA[myd$Individual==indiv]/2 * ((myd$P1==indiv) + (myd$P2==indiv))))
    

    Is what you are looking for something like this?

    # Initialize values of relationA
    myd$relationA <- 0
    myd$relationA[myd$Individual=="A"] <- 1
    
    
    # Iterate over all Individuals
    for (indiv in myd$Individual) {
    
      indiVal <- myd$relationA[myd$Individual==indiv]
    
      # all columns handled at once, thanks to vectorization;  no need for myd$P1[i]
      myd$relationA <- myd$relationA  + 
                     indiVal/2 * ((myd$P1==indiv) + (myd$P2==indiv))
    }
    

    Output

    myd
       Individual P1 P2 relationA
    1           A  0  0     1.000
    2           B  0  0     0.000
    3           C  A  B     0.500
    4           D  0  0     0.000
    5           E  C  D     0.250
    6           F  C  E     0.375
    7           G  0  0     0.000
    8          H1  E  G     0.125
    9          H2  E  G     0.125
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having difficulty executing a batch file in Java that expects parameters. These parameters
Im having difficulty trying to design a database structure for the following scenario: My
I am having difficulty getting rid if the following error: bash-3.00$ p4 login Enter
I am having difficulty creating sessions for my mobile application that is written in
I am having difficulty executing a MS SQL Server stored procedure from Java/jsp. I
I'm having difficulty testing some logic that uses notifications. I've read about enforcing that
This would seem to be a simple task but I'm having difficulty executing it
Following results are very interesting and I am having difficulty understanding them. Basically I
Im having difficulty in understanding how the pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)rowinComponent:(NSInteger)component works Ive set up
Having difficulty indexing an attachment type in elasticsearch via the Tire gem. Not able

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.