[R] Lag variable by group
Janka VANSCHOENWINKEL
janka.vanschoenwinkel at uhasselt.be
Tue Sep 8 09:32:34 CEST 2015
Wow! Thanks for pointing that out! And thanks for testing it out as well!
It is always the first year available (unbalanced panel) that should get NA.
So using the code line you provided earlier, this should work:
library(data.table)
data <- data.table(newdata, key = "id")
ooo<-order(data$id, data$year)
data <- data[ooo,]
data$lagvar<-data[, lag.t1:=c(NA, t1[-.N]), by=id]
Thank you very much for pointing that out!
2015-09-08 9:05 GMT+02:00 PIKAL Petr <petr.pikal at precheza.cz>:
> Hm. I tried your example but what puzzles me is that your data are not
> sorted by year and therefore sometimes the first year is changed to NA but
> sometimes any arbitrary year is changed to NA.
>
>
>
> > head(data)
>
> year id t1 lag.t1
>
> 1: 2007 28954 -1.818075 NA
>
> 2: 2006 28954 -1.818075 -1.818075
>
> 3: 2008 28954 -1.818075 -1.818075
>
> 4: 2005 28954 -1.818075 -1.818075
>
> 5: 2005 28955 -1.818075 NA
>
> 6: 2007 28955 -1.818075 -1.818075
>
>
>
> Is it what you intended?
>
> Cheers
>
> Petr
>
>
>
>
>
> *From:* Janka VANSCHOENWINKEL [mailto:janka.vanschoenwinkel at uhasselt.be]
> *Sent:* Tuesday, September 08, 2015 8:48 AM
> *To:* PIKAL Petr
> *Cc:* r-help at r-project.org
> *Subject:* Re: [R] Lag variable by group
>
>
>
> Hi Petr and other member who can use this post,
>
>
>
> Somebody gave me an answer in a private email which worked for me!
>
>
>
> The only thing I needed to do was to make first a data.table object of my
> data. Then the code works!
>
>
>
> library(data.table)
> data <- data.table(data, key = "id")
> data[, lag.t1:=c(NA, t1[-.N]), by=id]
>
>
>
> Thank you very much for your help Petr!
>
>
>
> I really appreciate it!
>
>
>
> Janka
>
>
>
>
>
>
>
> 2015-09-08 8:37 GMT+02:00 PIKAL Petr <petr.pikal at precheza.cz>:
>
> Hi
>
> Thanks for providing data. I did not see any response and frankly speaking
> I do not use data.table so I am not sure what do you mean by lagging t1.
>
> I would start with ordering data.
> ooo<-order(data$id, data$year)
> data <- data[ooo,]
>
> Then you can split data according to id.
>
> datas<-split(data[,c(1,3)], data$id)
>
> dput(head(datas))
> structure(list(`28954` = structure(list(year = c(2005, 2006,
> 2007, 2008), t1 = c(-1.81807494163513, -1.81807494163513,
> -1.81807494163513,
> -1.81807494163513)), .Names = c("year", "t1"), row.names = c(58L,
> 45L, 35L, 46L), class = "data.frame"), `28955` = structure(list(
> year = c(2005, 2006, 2007, 2008), t1 = c(-1.81807494163513,
> -1.81807494163513, -1.81807494163513, -1.81807494163513)), .Names =
> c("year",
> "t1"), row.names = c(59L, 70L, 69L, 72L), class = "data.frame"),
> `28956` = structure(list(year = c(2005, 2006, 2007, 2008),
> t1 = c(-1.81807494163513, -1.81807494163513, -1.81807494163513,
> -1.81807494163513)), .Names = c("year", "t1"), row.names = c(53L,
> 66L, 74L, 51L), class = "data.frame"), `28957` = structure(list(
> year = c(2005, 2006, 2007, 2008), t1 = c(-1.81807494163513,
> -1.81807494163513, -1.81807494163513, -1.81807494163513
> )), .Names = c("year", "t1"), row.names = c(71L, 64L,
> 54L, 24L), class = "data.frame"), `28958` = structure(list(
> year = c(2005, 2006, 2007, 2008), t1 = c(-1.81807494163513,
> -1.81807494163513, -1.81807494163513, -1.81807494163513
> )), .Names = c("year", "t1"), row.names = c(34L, 27L,
> 1L, 31L), class = "data.frame"), `28959` = structure(list(
> year = c(2005, 2006, 2007, 2008), t1 = c(-1.81807494163513,
> -1.81807494163513, -1.81807494163513, -1.81807494163513
> )), .Names = c("year", "t1"), row.names = c(17L, 18L,
> 30L, 44L), class = "data.frame")), .Names = c("28954", "28955",
> "28956", "28957", "28958", "28959"))
>
> But now I am lost what result you expect. Can you explain it on this
> smaller data set?
>
> Cheers
> Petr
>
>
> > -----Original Message-----
> > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Janka
> > VANSCHOENWINKEL
> > Sent: Monday, September 07, 2015 1:18 PM
> > To: r-help at r-project.org
> > Subject: [R] Lag variable by group
> >
> > Hi!
> >
> > I have the following dataset with the variables ID (this is a unique ID
> > per farmer), year, and another variable t1.
> > I now would like to have a fourth variable which is the lag value of t1
> > for each farm ID.
> >
> > I found a code on the internet that does exactly what I need, but it
> > does not work for this dataset. Does anyone have suggestions about how
> > I can make this work?
> >
> > Thanks a lot!
> >
> > Janka
> >
> > data<-structure(list(year = c(2007, 2005, 2008, 2006, 2005, 2007, 2006,
> > 2008, 2007, 2005, 2007, 2007, 2005, 2006, 2005, 2006, 2005, 2006, 2007,
> > 2007, 2005, 2008, 2007, 2008, 2005, 2005, 2006, 2008, 2007, 2007, 2008,
> > 2008, 2006, 2005, 2007, 2006, 2008, 2008, 2007, 2007, 2007, 2006, 2006,
> > 2008, 2006, 2008, 2008, 2008, 2006, 2007, 2008, 2007, 2005, 2007, 2008,
> > 2005, 2007, 2005, 2005, 2008, 2005, 2006, 2005, 2006, 2008, 2006, 2008,
> > 2006, 2007, 2006, 2005, 2008, 2006, 2007, 2008, 2006, 2006, 2006, 2005,
> > 2008, 2006, 2008, 2006, 2006, 2006, 2007, 2008, 2005, 2007, 2006, 2007,
> > 2008, 2006, 2008, 2005, 2007, 2005, 2007, 2006, 2006), id = c(28958L,
> > 28962L, 28962L, 28965L, 28960L, 28962L, 28964L, 28970L, 28961L, 28965L,
> > 78458L, 28960L, 28961L, 28961L, 28969L, 28962L, 28959L, 28959L, 58845L,
> > 28965L, 28963L, 78459L, 28967L, 28957L, 28964L, 28966L, 28958L, 28960L,
> > 28969L, 28959L, 28958L, 28969L, 58845L, 28958L, 28954L, 28963L, 78458L,
> > 28965L, 28966L, 28963L, 28970L, 28970L, 28960L, 28959L, 28954L, 28954L,
> > 58845L, 28967L, 28966L, 78459L, 28956L, 28964L, 28956L, 28957L, 28961L,
> > 28970L, 28968L, 28954L, 28955L, 28968L, 28968L, 28967L, 28967L, 28957L,
> > 28966L, 28956L, 28964L, 28969L, 28955L, 28955L, 28957L, 28955L, 28968L,
> > 28956L, 28963L, 29004L, 58848L, 29005L, 28974L, 29005L, 28974L, 29006L,
> > 28981L, 29007L, 29002L, 28980L, 29001L, 29006L, 29005L, 28989L, 28989L,
> > 58846L, 28980L, 28981L, 78467L, 28990L, 28973L, 29004L, 28972L,
> > 29006L), t1 = c(-1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.81807494163513, -1.81807494163513, -
> > 1.81807494163513, -1.43884992599487, -1.43884992599487, -
> > 1.43884992599487, -1.43884992599487, -1.43884992599487, -
> > 1.43884992599487, -1.43884992599487, -1.43884992599487, -
> > 1.43884992599487, -1.43884992599487, -1.43884992599487, -
> > 1.43884992599487, -1.43884992599487, -1.43884992599487, -
> > 1.43884992599487, -1.43884992599487, -1.43884992599487, -
> > 1.43884992599487, -1.43884992599487, -1.43884992599487, -
> > 1.43884992599487, -1.43884992599487, -1.43884992599487, -
> > 1.43884992599487, -1.43884992599487)), .Names = c("year", "id", "t1"),
> > row.names = c(NA, 100L), class = "data.frame")
> >
> > library(data.table)
> > data[, lag.t1:=c(NA, t1[-.N]), by=id]
> >
> >
> > Thank you very much!
> >
> > Janka
> >
>
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> ________________________________
> Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou
> určeny pouze jeho adresátům.
> Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě
> neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie
> vymažte ze svého systému.
> Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email
> jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
> Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi
> či zpožděním přenosu e-mailu.
>
> V případě, že je tento e-mail součástí obchodního jednání:
> - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření
> smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
> - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout;
> Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany
> příjemce s dodatkem či odchylkou.
> - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve
> výslovným dosažením shody na všech jejích náležitostech.
> - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za
> společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn
> nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto
> emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich
> existence je adresátovi či osobě jím zastoupené známá.
>
> This e-mail and any documents attached to it may be confidential and are
> intended only for its intended recipients.
> If you received this e-mail by mistake, please immediately inform its
> sender. Delete the contents of this e-mail with all attachments and its
> copies from your system.
> If you are not the intended recipient of this e-mail, you are not
> authorized to use, disseminate, copy or disclose this e-mail in any manner.
> The sender of this e-mail shall not be liable for any possible damage
> caused by modifications of the e-mail or by delay with transfer of the
> email.
>
> In case that this e-mail forms part of business dealings:
> - the sender reserves the right to end negotiations about entering into a
> contract in any time, for any reason, and without stating any reasoning.
> - if the e-mail contains an offer, the recipient is entitled to
> immediately accept such offer; The sender of this e-mail (offer) excludes
> any acceptance of the offer on the part of the recipient containing any
> amendment or variation.
> - the sender insists on that the respective contract is concluded only
> upon an express mutual agreement on all its aspects.
> - the sender of this e-mail informs that he/she is not authorized to enter
> into any contracts on behalf of the company except for cases in which
> he/she is expressly authorized to do so in writing, and such authorization
> or power of attorney is submitted to the recipient or the person
> represented by the recipient, or the existence of such authorization is
> known to the recipient of the person represented by the recipient.
>
>
>
>
>
> -
>
>
>
> ------------------------------
> Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou
> určeny pouze jeho adresátům.
> Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě
> neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie
> vymažte ze svého systému.
> Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email
> jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
> Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi
> či zpožděním přenosu e-mailu.
>
> V případě, že je tento e-mail součástí obchodního jednání:
> - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření
> smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
> - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout;
> Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany
> příjemce s dodatkem či odchylkou.
> - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve
> výslovným dosažením shody na všech jejích náležitostech.
> - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za
> společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn
> nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto
> emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich
> existence je adresátovi či osobě jím zastoupené známá.
>
> This e-mail and any documents attached to it may be confidential and are
> intended only for its intended recipients.
> If you received this e-mail by mistake, please immediately inform its
> sender. Delete the contents of this e-mail with all attachments and its
> copies from your system.
> If you are not the intended recipient of this e-mail, you are not
> authorized to use, disseminate, copy or disclose this e-mail in any manner.
> The sender of this e-mail shall not be liable for any possible damage
> caused by modifications of the e-mail or by delay with transfer of the
> email.
>
> In case that this e-mail forms part of business dealings:
> - the sender reserves the right to end negotiations about entering into a
> contract in any time, for any reason, and without stating any reasoning.
> - if the e-mail contains an offer, the recipient is entitled to
> immediately accept such offer; The sender of this e-mail (offer) excludes
> any acceptance of the offer on the part of the recipient containing any
> amendment or variation.
> - the sender insists on that the respective contract is concluded only
> upon an express mutual agreement on all its aspects.
> - the sender of this e-mail informs that he/she is not authorized to enter
> into any contracts on behalf of the company except for cases in which
> he/she is expressly authorized to do so in writing, and such authorization
> or power of attorney is submitted to the recipient or the person
> represented by the recipient, or the existence of such authorization is
> known to the recipient of the person represented by the recipient.
>
--
[image: Logo UHasselt]Mevrouw Janka Vanschoenwinkel
*Doctoraatsbursaal - PhD *
Milieueconomie - Environmental economics
T +32(0)11 26 87 42 | GSM +32(0)476 28 21 40
www.uhasselt.be/eec
Universiteit Hasselt | Campus Diepenbeek
Agoralaan Gebouw D | B-3590 Diepenbeek
Kantoor F11
Postadres: Universiteit Hasselt | Martelarenlaan 42 | B-3500 Hasselt
P Please consider the environment before printing this e-mail
[[alternative HTML version deleted]]
More information about the R-help
mailing list