Sorkin, John
2024-Nov-29 01:25 UTC
[R] Trying to get the prior value of a record from a data.frame . . . data.frame
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
Kimmo Elo
2024-Nov-29 06:59 UTC
[R] Trying to get the prior value of a record from a data.frame . . . data.frame
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 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
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.elo at uef.fi
ResearchGate: http://www.researchgate.net/profile/Kimmo_Elo
LAWPOL Consortium (PI): https://lawpol.fi/en
========================================================
Rui Barradas
2024-Nov-29 08:33 UTC
[R] Trying to get the prior value of a record from a data.frame . . . data.frame
?s 01:25 de 29/11/2024, Sorkin, John escreveu:> 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 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 https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, If you want to shift an entire vector, you don't need the loop, pass the vector itself to data.table::shift. x <- data.frame(Id=rep(1:10),num=rep(11:20)) data.table::shift(x[["num"]], n = 1L) #> [1] NA 11 12 13 14 15 16 17 18 19 data.table::shift(x[, "num"], n = 1L) #> [1] NA 11 12 13 14 15 16 17 18 19 Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
Bert Gunter
2024-Nov-30 20:21 UTC
[R] Trying to get the prior value of a record from a data.frame . . . data.frame
I assume that the responses that John already received to his recent
post met his needs. However, when I read it, I had a slightly
different interpretation. So feel free to ignore the rest of this post
if you like, but here's my interpretation and a simple solution to it.
An example to help explain:
set.seed(453)
df <- data.frame(
group = sample(letters[1:4],30, rep = TRUE),
gender = sample(c("M", "F", "NB"), 30, rep =
TRUE),
value = 1:30
)
df is a data frame with 30 records/rows of 3 columns giving a group
identifier, gender, and value for each record. I kept the values
artificially simple to (I hope) make it easier to understand the
problem and my solution.
The problem: create a new column, prev.value, that gives the value of
the previous record that has the same group and gender as the current
record if any such exist; or NA if no such previous id and gender
combination occur.
I think this is a slightly more complex task than John's original
request, but it is actually straightforward even in base R -- and
undoubtedly also using similar functionality in the Tidyverse or other
packages.
Here is my solution (using the R pipe, "|>", syntax):
df$prev.value <- with(df, {
f <- paste0(group,gender) ## a simple 'hash' to identify the
combinations
## "shift" the values for each group defined by f:
f |> tapply(value, INDEX = _, FUN = \(x)c(NA, head(x, -1))) |>
## reassemble according to f:
unsplit(f)
})
df
Cheers,
Bert
On Thu, Nov 28, 2024 at 5:25?PM Sorkin, John <jsorkin at
som.umaryland.edu> wrote:>
> 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 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
https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Reasonably Related Threads
- Convert character date time to R date-time variable.
- R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments
- ggplot with major and MINOR tick marks on a log scale
- Convert string to date time
- Why is there no macro facility for R?