# [R] Data framing question

arun smartpink111 at yahoo.com
Sat Jun 7 10:07:08 CEST 2014

```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)
#  TRUE
identical(dt1,dt3)
# TRUE

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

A.K.

On Friday, June 6, 2014 10:47 PM, arun <smartpink111 at yahoo.com> wrote:
Hi,
May be this helps:
1 7 5
2 8 9
2 9 8
2 10 7
3 11 6
3 1 4
3 2 2
4 3 3

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

```