Hi,
You may also try:
library(data.table)
dt1 <- data.table(dat1, key = "identif")
dt2 <- copy(dt1)
dt3 <- copy(dt1)
dt1[,c("roa1", "eta1") := list(c(NA, roa[-.N]),c(NA, diff(eta))), by=identif]
#or
dt2[, `:=`(c("roa1", "eta1"), list(c(NA, roa[-.N]), c(NA, diff(eta, by =
identif]
# or
dt3[,
Hi,
May be this helps:
dat1 <- read.table(text="identif roa eta
1 7 5
2 8 9
2 9 8
2 10 7
3 11 6
3 1 4
3 2 2
4 3 3
4 6 5",sep="",header=TRUE)
dat2 <-within(dat1, {
eta1 <- ave(eta, identif, FUN = function(x) c(NA, diff(x)))
roa1 <- ave(roa, identif, FUN = function(x) c(NA, x[-length(x)]))
2 matches
Mail list logo