我在执行迭代定义的计算时遇到困难。以下数据作为示例(实际数据集更大):
## 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
数据表示 和 个人与两个父母之间的关系,P1
,P2
。
所需的计算,标记为relationA
,表示每个人与 A 的相关程度。
根据定义,A 和 A 之间的关系被赋值为 1。所有其他个体的值需要根据表中的信息计算,如下所示:
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)
例如
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
预期的输出如下:
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
我的困难在于以适当的方式表达这一点R
。任何帮助,将不胜感激。