Hi, is there a specific reason to use "shift"? I mean, you could easily achieve what you described by simple indexing:
--- snjp --- for (i in 1:10) { cat("x[i,num]",x[i,"num"],"\n") # Get previous value of x[i,"num"] zoop<-x[i-1,"num"] # NB! Returns "integer(0)" for row index 0 cat("Previous value of x[,num]=",zoop,"\n") } --- snip --- The code above just loops through the *rows* from 1 to 10 and dumps the "num" value from the previous row. If you want to get the "num" value from the row above a specific "Id", then the zoop-line should look something like this: zoop<-x[which(x["Id"]==i)-1, "num"] HTH, Kimmo Sorkin, John kirjoitti 29.11.2024 klo 3.25: > I need to write code that will give me the previous value of from a > data.frame. I have written the following code using the shift function from > data.table . It does not work. I hope someone can help me correct the code. > ########################### > # Try to understand shift # > ########################### > if(!require(data.table)) install.packages("data.table") > library(data.table) > # Create data > x <- data.frame(Id=rep(1:10),num=rep(11:20)) > cat("This is the input data.frame used in the code below","\n") > x > > for (i in 1:10) { > cat("x[i,num]",x[i,"num"],"\n") > # Get previous value of x[i,"num"] > zoop<-shift(x[i,"num"], n=1L, type="lag") > cat("Previous value of x[,num]=",zoop,"\n") > } > ############################### > # END Try to understand shift # > ############################### > > Thank you, > John > > > John David Sorkin M.D., Ph.D. > Professor of Medicine, University of Maryland School of Medicine; > Associate Director for Biostatistics and Informatics, Baltimore VA Medical > Center Geriatrics Research, Education, and Clinical Center; > PI Biostatistics and Informatics Core, University of Maryland School of > Medicine Claude D. Pepper Older Americans Independence Center; > Senior Statistician University of Maryland Center for Vascular Research; > > Division of Gerontology and Paliative Care, > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > Cell phone 443-418-5382 > > > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide https://www.r-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Kimmo Elo Senior Lecturer | Adjunct professor, Dr. ======================================================== University of Eastern Finland Department of Geographical and Historical Studies P.O. Box 111 FIN-80101 Joensuu Finland E-mail: kimmo....@uef.fi ResearchGate: http://www.researchgate.net/profile/Kimmo_Elo LAWPOL Consortium (PI): https://lawpol.fi/en ======================================================== ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.