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[, `:=`(roa1 = c(NA, roa[-.N]), eta1 = c(NA, diff(eta))), by = identif]

identical(dt1, dt2)
# [1] TRUE
identical(dt1,dt3)
#[1] TRUE


identical(dat2, as.data.frame(dt1))
# [1] TRUE

A.K.


On Friday, June 6, 2014 10:47 PM, arun <smartpink...@yahoo.com> wrote:
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)]))
})

A.K.


So here is my little problem. I don't know how to compute a new variable roa1, 
such that first data is split by identif then roa 1 will take the value of the 
line just before. so it will be something like roa1=(NA,NA,8,9,NA,11,1,NA,3)
 and the same for eta just that for this one first data is split by identif 
then deta will take the value of the difference between the actual value of eta 
with the previous something like (NA,NA,-1,-1,NA,-2,2,NA,-2)
21 mins · Like

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to