Hi I found an example in R to create a lagged panel data set which works fine. The only problem is that it adds the lagged variable as follows wage2.dat year person wage lag(wage, -1) 1.1 1980 1 -0.75843997 NA 1.2 1981 1 0.27233048 -0.75843997 1.3 1982 1 -1.58335767 0.27233048 1.4 1983 1 0.36805926 -1.58335767 1.5 1984 1 -0.52312153 0.36805926 2.6 1980 2 -0.53559110 NA 2.7 1981 2 -0.94935350 -0.53559110 2.8 1982 2 0.10486688 -0.94935350 2.9 1983 2 -0.50266443 0.10486688 2.10 1984 2 0.14644024 -0.50266443 . . Is there anyway I could rename the last column wag.lag1? Thanks. Best, Alok
R. Michael Weylandt <michael.weylandt@gmail.com>
2012-Aug-29 18:29 UTC
[R] creating lagged variable in panel data
Of course: colnames(dats)[4] <- "new name" M On Aug 29, 2012, at 9:34 AM, "Alok K Bohara, PhD" <bohara at unm.edu> wrote:> Hi > > I found an example in R to create a lagged panel data set which works fine. The only problem is that it adds the lagged variable as follows > > wage2.dat > > year person wage lag(wage, -1) > 1.1 1980 1 -0.75843997 NA > 1.2 1981 1 0.27233048 -0.75843997 > 1.3 1982 1 -1.58335767 0.27233048 > 1.4 1983 1 0.36805926 -1.58335767 > 1.5 1984 1 -0.52312153 0.36805926 > 2.6 1980 2 -0.53559110 NA > 2.7 1981 2 -0.94935350 -0.53559110 > 2.8 1982 2 0.10486688 -0.94935350 > 2.9 1983 2 -0.50266443 0.10486688 > 2.10 1984 2 0.14644024 -0.50266443 > . > . > > Is there anyway I could rename the last column wag.lag1? Thanks. > > > Best, > Alok > > ______________________________________________ > R-help at 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.
HI, Try this: wage2.dat<-read.table(text=" ? year person?? wage??????? lag(wage,-1) ? 1980????? 1 -0.75843997??????????? NA ? 1981????? 1? 0.27233048? -0.75843997 ? 1982????? 1 -1.58335767??? 0.27233048 ? 1983????? 1? 0.36805926? -1.58335767 ? 1984????? 1 -0.52312153??? 0.36805926 ? 1980????? 2 -0.53559110??????????? NA ? 1981????? 2 -0.94935350? -0.53559110 ? 1982????? 2? 0.10486688? -0.94935350 ? 1983????? 2 -0.50266443??? 0.10486688 ?1984????? 2? 0.14644024? -0.50266443 ",sep="",header=TRUE) ?row.names(wage2.dat)<-c(paste0(1,".",1:5),paste0(2,".",6:10)) names(wage2.dat)[4]<-"wag.lag1" A.K. ----- Original Message ----- From: "Alok K Bohara, PhD" <bohara at unm.edu> To: R-help at r-project.org Cc: Sent: Wednesday, August 29, 2012 10:34 AM Subject: [R] creating lagged variable in panel data Hi I found an example in R to create a lagged panel data set which works fine.? The only problem is that it adds the lagged variable as follows wage2.dat ? ? ? ? ? year person? ? ? ? wage? ? ? ? lag(wage, -1) 1.1? 1980? ? ? 1 -0.75843997? ? ? ? ? ? NA 1.2? 1981? ? ? 1? 0.27233048? -0.75843997 1.3? 1982? ? ? 1 -1.58335767? ? 0.27233048 1.4? 1983? ? ? 1? 0.36805926? -1.58335767 1.5? 1984? ? ? 1 -0.52312153? ? 0.36805926 2.6? 1980? ? ? 2 -0.53559110? ? ? ? ? ? NA 2.7? 1981? ? ? 2 -0.94935350? -0.53559110 2.8? 1982? ? ? 2? 0.10486688? -0.94935350 2.9? 1983? ? ? 2 -0.50266443? ? 0.10486688 2.10 1984? ? ? 2? 0.14644024? -0.50266443 . . Is there anyway? I could rename the last column? ? ? wag.lag1? Thanks. Best, Alok ______________________________________________ R-help at 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.
Thanks for the input. I wanted to avoid counting the column number. In any case, in the script -- wage.lag1 = lag(wage, -1) seems to do the trick. Alok On 8/29/2012 12:29 PM, R. Michael Weylandt <michael.weylandt at gmail.com> wrote:> Of course: > > colnames(dats)[4] <- "new name" > > M > > On Aug 29, 2012, at 9:34 AM, "Alok K Bohara, PhD" <bohara at unm.edu> wrote: > >> Hi >> >> I found an example in R to create a lagged panel data set which works fine. The only problem is that it adds the lagged variable as follows >> >> wage2.dat >> >> year person wage lag(wage, -1) >> 1.1 1980 1 -0.75843997 NA >> 1.2 1981 1 0.27233048 -0.75843997 >> 1.3 1982 1 -1.58335767 0.27233048 >> 1.4 1983 1 0.36805926 -1.58335767 >> 1.5 1984 1 -0.52312153 0.36805926 >> 2.6 1980 2 -0.53559110 NA >> 2.7 1981 2 -0.94935350 -0.53559110 >> 2.8 1982 2 0.10486688 -0.94935350 >> 2.9 1983 2 -0.50266443 0.10486688 >> 2.10 1984 2 0.14644024 -0.50266443 >> . >> . >> >> Is there anyway I could rename the last column wag.lag1? Thanks. >> >> >> Best, >> Alok >> >> ______________________________________________ >> R-help at 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.-- Alok K. Bohara, Ph.D. Professor Department of Economics MSC 05 3060 1 University of New Mexico Albuquerque, NM 87131-0001, USA Ph: 505-277-5903/5304(w) Fax:505-277-9445 email: bohara at unm.edu http://www.unm.edu/~econ/faculty/professors.html Nepal Study Center: http://nepalstudycenter.unm.edu