[R] Using R to Calculate Coefficients of Relatedness and Kinship for Family Members
JS Huang
js.huang at protective.com
Fri Mar 6 03:50:39 CET 2015
Hi,
I don't quite understand the logic since some later assignment will
overwrite the previous one. I simply assign for the current index and
ignore the index after it. In addition, the column Dads.Renamed is not in
the data.frame you defined in the post and I used Father_ID to substitute
for it. Here is an implementation. Write back if this is not what you
expect.
> pedigree.data
Subject Twin_Stat Zygosity Father_ID Mother_ID
1 100307 Twin NotMZ 81352 51488
2 100408 Twin MZ 81594 51730
3 101006 Twin MZ 81149 51283
4 101107 NotTwin NotTwin 81833 51969
5 101309 NotTwin NotTwin 82248 52385
6 101410 Twin NotMZ 82061 52198
7 101915 NotTwin NotTwin 81841 51977
8 102008 NotTwin NotTwin 81882 52018
9 102311 Twin MZ 81543 51679
10 102816 Twin MZ 81283 51418
> assignKinScore
function(Input)
{
result <- rep(0,dim(Input)[1] - 1)
for (i in 1:(dim(Input)[1] - 1))
{
if (as.character(Input$Twin_Stat[i]) == "Twin" &
as.character(Input$Twin_Stat[i + 1]) == "Twin")
{
if (as.character(Input$Zygosity[i]) == "MZ" &
as.character(Input$Zygosity[i + 1]) == "MZ")
{
result[i] <- 0.5
}
else
{
if (as.character(Input$Zygosity[i]) == "DZ" &
as.character(Input$Zygosity[i + 1]) == "DZ")
{
result[i] <- 0.25
}
}
}
else
{
if (as.character(Input$Twin_Stat[i])=="NotTwin" &
as.character(Input$Twin_Stat[i + 1])=="NotTwin")
{
if (Input$Mother_ID[i] == Input$Mother_ID[i + 1] &
Input$Father_ID[i] == Input$Father_ID[i + 1])
{
result[i] <- 0.25
}
else
{
if (Input$Mother_ID[i] == Input$Mother_ID[i + 1] &
Input$Father_ID[i] != Input$Father_ID[i + 1])
{
result[i] <- 0.125
}
}
}
}
}
return(result)
}
> assignKinScore(pedigree.data)
[1] 0.0 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.5
-----
JS Huang
--
View this message in context: http://r.789695.n4.nabble.com/Using-R-to-Calculate-Coefficients-of-Relatedness-and-Kinship-for-Family-Members-tp4704232p4704243.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list