Plyr solution:
library(plyr)
ddply(df,.(ID),transform,AGE_HEAD=head(AGE,1))
OR
ddply(df,.(ID),transform,AGE_HEAD=AGE[PERNO==1])
ID AGE PERNO AGE_HEAD
1 1 30 1 30
2 1 25 2 30
3 2 25 1 25
4 2 24 2 25
5 2 3 3 25
6 3 65 1 65
7 3 55 2 65
data.table solution:
library(data.table)
DT<-data.table(df)
DT[, AGE_HEAD := AGE[PERNO==1], by="ID"]
ID AGE PERNO AGE_HEAD
1: 1 30 1 30
2: 1 25 2 30
3: 2 25 1 25
4: 2 24 2 25
5: 2 3 3 25
6: 3 65 1 65
7: 3 55 2 65
5
solved Calculate variable based on criteria in r