You can do this for example with the package dplyr
:
require(dplyr) #install the package dplyr and load it into the library
df <- df %.% #your data.frame
group_by(names) %.%
mutate(count = 1:n()) %.%
mutate(x1.absdif = ifelse(count==1,NA,diff(x1)),
x2.absdif = ifelse(count==1, NA,diff(x2)),
x3.absdif = ifelse(count==1, NA,diff(x3)),
x1.pcndif = ifelse(count==1, NA, x1.absdif/lag(x1,1)),
x2.pcndif = ifelse(count==1, NA, x2.absdif/lag(x2,1)),
x3.pcndif = ifelse(count==1, NA, x3.absdif/lag(x3,1))) %.%
select(-count)
output:
#>df
# names x1 x2 x3 x1.absdif x2.absdif x3.absdif x1.pcndif x2.pcndif x3.pcndif
#1 A 1 11 21 NA NA NA NA NA NA
#2 A 2 12 22 1 1 1 1.0000000 0.09090909 0.04761905
#3 A 3 13 23 1 1 1 0.5000000 0.08333333 0.04545455
#4 A 4 14 24 1 1 1 0.3333333 0.07692308 0.04347826
#5 B 5 15 25 NA NA NA NA NA NA
#6 B 6 16 26 1 1 1 0.2000000 0.06666667 0.04000000
#7 B 7 17 27 1 1 1 0.1666667 0.06250000 0.03846154
#8 C 8 18 28 NA NA NA NA NA NA
#9 C 9 19 29 1 1 1 0.1250000 0.05555556 0.03571429
#10 C 10 20 30 1 1 1 0.1111111 0.05263158 0.03448276
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…