不同行的不同年龄计算

时间:2017-05-23 15:16:54

标签: r

我是一名绝对的R初学者,在这里从事硕士项目。

我有一个data.frame,其中包含有关小跑马的信息(他们的胜利,收入,时间记录等)。数据的组织方式是每一行都包含马竞争的特定年份的信息,并包括每匹马“总计”的第一行,因此对于每个变量的总竞争生命有一个总结。它看起来像这样:

Data example 我使用eeptools包中的age_calc函数创建了一个具有年龄的新变量:

travdata$Age<-age_calc(as.Date(travdata$Birth.date), enddate=as.Date("2016-12-31"),
                       units="years")

没有问题。我想弄清楚的是,如果有任何方法我可以计算每个特定年份的马的年龄我有他们的信息 - 也就是说,“总计”排的年龄将一直到2016-12- 31,对于2015年,它将具有当时的年龄,依此类推。我一直试图在age_calc中包含if语句,但它不起作用,我真的不知道如何最好地做到这一点。

你可以指出我的任何文献或帮助都会非常感激。

MWE

travdata <- data.frame(
    "Id.Number"=c(rep("1938-98",3),rep("1803-97",7),rep("1221-03",4)),
    "Name"=c(rep("Muuttuva",3),rep("Pelson Poika",7),rep("Muusan Muisto",4)),
    "Sex"=c(rep("Mare",3),rep("Gelding",7),rep("Gelding",4)),
    "Birth.year"=c(rep(1998,3),rep(1997,7),rep(2003,4)),
    "Birth.date"=c(rep("1998-07-01",3),rep("1997-07-14",7),rep("2003-05-07",4)),
    "Competition.year" = c("Total",2005,2004,"Total",2003,2004,2006,2005,2002,2001,2008,2010,"Total",2009),
    "starts"=c(20,11,9,44,21,6,7,5,3,2,1,1,4,2),
    "X1st.placements"=c(0,0,0,3,3,0,0,0,0,0,0,0,0,0),
    "X2nd.placements"=c(2,2,0,1,0,1,0,0,0,0,0,0,0,0),
    "X3rd.placements"=c(2,2,0,1,1,0,0,0,0,0,0,0,0,0),
    "Earnings.euro"=c(1525,1425,100,2078,1498,580,0,0,0,0,0,0,10,10)
)

1 个答案:

答案 0 :(得分:0)

诀窍是过滤掉&#34; Total&#34;行并指定as.Date()函数的格式

library(eeptools)
travdata <- data.frame(
    "Id.Number"=c(rep("1938-98",3),rep("1803-97",7),rep("1221-03",4)),
    "Name"=c(rep("Muuttuva",3),rep("Pelson Poika",7),rep("Muusan Muisto",4)),
    "Sex"=c(rep("Mare",3),rep("Gelding",7),rep("Gelding",4)),
    "Birth.year"=c(rep(1998,3),rep(1997,7),rep(2003,4)),
    "Birth.date"=c(rep("1998-07-01",3),rep("1997-07-14",7),rep("2003-05-07",4)),
    "Competition.year" = c("Total",2005,2004,"Total",2003,2004,2006,2005,2002,2001,2008,2010,"Total",2009),
    "starts"=c(20,11,9,44,21,6,7,5,3,2,1,1,4,2),
    "X1st.placements"=c(0,0,0,3,3,0,0,0,0,0,0,0,0,0),
    "X2nd.placements"=c(2,2,0,1,0,1,0,0,0,0,0,0,0,0),
    "X3rd.placements"=c(2,2,0,1,1,0,0,0,0,0,0,0,0,0),
    "Earnings.euro"=c(1525,1425,100,2078,1498,580,0,0,0,0,0,0,10,10)
)

travdata$Age<-age_calc(as.Date(travdata$Birth.date), 
                       enddate=as.Date("2016-12-31"), units="years")

competitions <- travdata[travdata$Competition.year!="Total",]
competitions$Competition.age<-age_calc(
                 as.Date(competitions$Birth.date),
                 enddate=as.Date(competitions$Competition.year, format="%Y"), 
                 units="years",F)