Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine elements from a list and ensure only the latest record is used when applicable?

Tags:

r

purrr

I am working with a series of data.frames in a list, where each round adds a new year and drops the last year, as shown in the picture below:

enter image description here

Here is the R code for the minimal reproducible example:

library(purrr)

# Define the year ranges:
(year_ranges <- map(0:2, \(increment) {(1991:1995) + increment}))


# Create data.frames:
(df_1 <- map(year_ranges, \(year_range) {
  
  map_dfc(year_range, \(col) {setNames(list(rnorm(n = 4)), as.character(col))})
  
}))

I'd like to combine them into one data.frame, and only keep the data from the latest round if multiple records exist:

enter image description here

For example, year 1992 has 2 rounds of records, only the newer one will be used (marked as light green) and the older ones will be abandoned.

How can I achieve this? Any comments are welcome.

like image 845
Grasshopper_NZ Avatar asked Nov 07 '25 06:11

Grasshopper_NZ


1 Answers

Here is a base R approach using duplicated() to subset the last occurrence of each name:

dat <- do.call(cbind, df_1)
dat[!duplicated(names(dat), fromLast = TRUE)]
#         1991       1992       1993        1994      1995       1996       1997
# 1 -2.5991600 -0.9621779  0.7901328 -0.51441785 1.6508529 -1.2604506  1.3905070
# 2 -0.8418027  0.9187921  0.1650032 -0.05987334 0.5572070 -2.0478516 -0.1336267
# 3  0.6176185  2.1625797  1.5277426 -1.60377711 0.1383535 -0.2871436 -0.3338140
# 4 -1.7544975 -0.7335684 -0.6778189  0.01732084 0.0369754  0.9178328  0.8352614
like image 116
SamR Avatar answered Nov 09 '25 08:11

SamR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!