[R] Data framing question

arun smartpink111 at yahoo.com
Sat Jun 7 04:47:54 CEST 2014


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 




More information about the R-help mailing list