Will S
Will S

Reputation: 13

Repeat measures: how to use initial measurements to estimate subsequent measurement based off time differences

I have a dataframe with repeat recordings of individuals in the year they were found.

>long<-data.frame(identity,year,age)
> long
   identity year  age
1         z 2000 10.0
2         z 2001  7.5
3         z 2001  7.5
4         y 2000 10.0
5         x 2003  9.0
6         x 2004 11.0
7         w 2003  9.0
8         v 2001  7.5
9         v 2002 11.0
10        v 2004 11.0

Age was estimated based off the year they were captured

yr.est<-data.frame(yr,est.age)
> yr.est
    yr est.age
1 2000    10.0
2 2001     7.5
3 2002    11.0
4 2003     9.0
5 2004    11.0

When an individual is seen after the first time how do I give them an estimated age of the initial estimated age + difference between years (e.g. individual v was estimated to be 7.5 in 2001 and their age in 2004 should be 10.5 not 11)

My actual dataset is 15000 long so I am unable to do it manually

TIA

Edit.

Expected output posted as a comment by the OP.

long 
  identity year age 
1        z 2000 10.0 
2        z 2001 11.0 
3        z 2001 11.0 
4        y 2000 10.0 
5        x 2003 9.0 
6        x 2004 10.0 
7        w 2003 9.0 
8        v 2001 7.5 
9        v 2002 8.5 
10       v 2004 10.5

Upvotes: 1

Views: 38

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76450

This code computes est.age by adding to the first age the difference between the current year and the first year, by group of identity.

library(tidyverse)

long %>%
  group_by(identity) %>%
  mutate(est.age = first(age) + (year - first(year))) %>%
  select(identity, year, est.age)
## A tibble: 10 x 3
## Groups:   identity [5]
#   identity  year est.age
#   <fct>    <int>   <dbl>
# 1 z         2000    10  
# 2 z         2001    11  
# 3 z         2001    11  
# 4 y         2000    10  
# 5 x         2003     9  
# 6 x         2004    10  
# 7 w         2003     9  
# 8 v         2001     7.5
# 9 v         2002     8.5
#10 v         2004    10.5

Data.

long <- read.table(text = "
   identity year  age
1         z 2000 10.0
2         z 2001  7.5
3         z 2001  7.5
4         y 2000 10.0
5         x 2003  9.0
6         x 2004 11.0
7         w 2003  9.0
8         v 2001  7.5
9         v 2002 11.0
10        v 2004 11.0
", header = TRUE)

Upvotes: 1

Related Questions