如何计算R中后代的平均出生年数? [英] How to calculate the average birthyear of offspring in R?

查看:42
本文介绍了如何计算R中后代的平均出生年数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道每个人的直接后代的平均出生年限.包谱系"具有一个函数(countOff),可以计算所有后代,包括大后代,这不是我想要的.这是我的数据框(df):

I would like to know the average birthyear of the direct offspring per individual. The package 'pedigree'has a function (countOff) that calculates all offspring, including the grand-offspring, which is not what I want. This is my dataframe (df):

ID <- 1:30
MomID <- c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 1,2,1,2,6,8,6,10,11,13,16,19,16,13,16,20,19,16,19,20,23)
DadID <- c(NA, NA, NA, NA, NA, NA, NA, NA, NA,3,4,5,5,7,4,9,7,7,14,18,7,15,18,18,17,21,14,18,21,17)
Birthyear <- c(1975, 1975, 1976, 1977, 1977, 1977, 1977, 1978, 1978, 1980, 1981, 1982, 1982, 1984, 1984, 1985, 1985, 1985, 1988, 1989, 1990, 1990, 1991, 1992, 1993, 1993, 1993, 1995, 1995, 1996)
df <- dataframe(ID,MomID, DadID, Birthyear)

   ID MomID DadID Birthyear
1   1    NA    NA      1975
2   2    NA    NA      1975
3   3    NA    NA      1976
4   4    NA    NA      1977
5   5    NA    NA      1977
6   6    NA    NA      1977
7   7    NA    NA      1977
8   8    NA    NA      1978
9   9    NA    NA      1978
10 10     1     3      1980
11 11     2     4      1981
12 12     1     5      1982
13 13     2     5      1982
14 14     6     7      1984
15 15     8     4      1984
16 16     6     9      1985
17 17    10     7      1985
18 18    11     7      1985
19 19    13    14      1988
20 20    16    18      1989
21 21    19     7      1990
22 22    16    15      1990
23 23    13    18      1991
24 24    16    18      1992
25 25    20    17      1993
26 26    19    21      1993
27 27    16    14      1993
28 28    19    18      1995
29 29    20    21      1995
30 30    23    17      1996

要做到这一点,我需要为每个人计算他们拥有的后代数量,然后平均这些后代的出生年限.每个人的后代数量可以是数据框中的另一列,offpsring的平均出生年份也是如此.有什么想法吗?

To do this I would need to count for every individual the number of offspring they have, and then average the birthyears of those offspring. The number of offspring for each individual can be another column in the dataframe, same goes for the average birthyear of the offpsring. Any ideas?

推荐答案

您可以使用 sapply 中的 mean 和子集 df $ Birthyear 来使用 df $ ID :

You can use mean in sapply and subset df$Birthyear using df$ID:

df$AvBirthYear <- sapply(df$ID, function(i) mean(df$Birthyear[df$MomID == i |
           df$DadID==i], na.rm=TRUE))
 head(df)
#  ID MomID DadID Birthyear AvBirthYear
#1  1    NA    NA      1975      1981.0
#2  2    NA    NA      1975      1981.5
#3  3    NA    NA      1976      1980.0
#4  4    NA    NA      1977      1982.5
#5  5    NA    NA      1977      1982.0
#6  6    NA    NA      1977      1984.5

这篇关于如何计算R中后代的平均出生年数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆