oh sorry i forgot to mention that it's a panel, so the lag must be by "id" as in the example. it's not a simple time series, but the first lagged entry for each "id" must be NA.
On 28 May 2014 15:45, Florian Oswald <[email protected]> wrote: > Hi > > I'm looking for the easiest way to create a lagged variable in a > dataframe. I'm almost there with this: > > df = DataFrame(id=repeat([1:3],inner=[3],outer=[1]),time=repeat([1:3], > inner=[1],outer=[3]),y=rand(9)) > by(df2, :id , d -> DataFrame(time=d[:,:time],y=d[:,:y],Ly=[0,d[1:end-1,:y > ]])) > > but of course instead of the 0.0 for the first entry for each id I would > like to have an NA: > > by(df, :id , d -> DataFrame(time=d[:,:time],y=d[:,:y],Ly=[NA,d[1:end-1,:y > ]])) > > but I cant figure out how to pass a valid data type here. thsi says > "ERROR: no method convert(Type{Float64}, NAtype)" > > i tried also this > > by(df, :id , d -> DataFrame(time=d[:,:time],y=d[:,:y],Ly=@data([NA,d[1:end > -1,:y]]))) > > but that gives "ERROR: no method DataArray{T,N}(DataArray{Float64,1}, > Array{Bool,1})". > > >
