如何根据时间顺序日期在未命名列表的中间插入元素

时间:2015-12-15 19:39:00

标签: r list

我已经获得了一个列表列表,并且正在寻找一种基于时间顺序将新列表插入此列表的适当方法。这是一个例子:

my_list <- list(list("text" = list("headline" = "Hello, World!", 
                                   "text" = "This is some text"), 
                                   "start_date" = list("year" = 2015, 
                                                       "month" = "01", 
                                                       "day" = "01")), 
                list("text" = list("headline" = "Hola, Mundo!", 
                                   "text" = "Este es algo palabras"), 
                                   "start_date" = list("year" = 2015, 
                                                       "month" = "01", 
                                                       "day" = "03")))

现在,如果我想在此列表中添加一个新元素,例如,start_date是2015-01-02,我想将它追加到索引2的列表中间并推送第二个元素&#34; down&#34;。如果start_date是2014-12-31,那么我一开始就想要它并且推动其他所有内容&#34; down&#34;,如果它在2015-01-03之后是什么我会...最后想要它。是否有更有效的方法来解决这个问题?

new_list_item <- list("text" = list("headline" = "Bonjour le monde", "text" = "ceci est un texte"), "start_date" = list("year" = 2015, "month" = "01", "day" = "02"))
counter <- 0
index <- lapply(my_list, function(elem) {
  date1 <- as.Date(paste(elem$start_date$year, elem$start_date$month, elem$start_date$day, sep = "-"))
  date2 <- as.Date(paste(new_list_item$start_date$year, new_list_item$start_date$month, new_list_item$start_date$day, sep = "-"))
  counter <<- counter + 1
  if (date2 > date1) {
    return(NULL)
  } else {
    return(counter)
  }
})
index <- min(unlist(index)[!is.null(index)])
my_list <- list(my_list[1:(index - 1)], new_list_item, my_list[index:length(my_list)])

特别是因为上述方法在列表元素上添加了额外的索引(即[[1]][[1]]$text vs [[1]]$text),这不是理想的。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

如果您像这样定义new_list_item(以匹配my_list的结构):

new_list_item <- list(list("text" = list("headline" = "Bonjour le monde", "text" = "ceci est un texte"), "start_date" = list("year" = 2015, "month" = "01", "day" = "02")))

然后以下功能起作用:

insert_new_list_item <- function(old_list, new_item){
    # Get the date from new_item
    new_item_date <- as.Date(paste(new_item[[1]]$start_date$year, 
                             new_item[[1]]$start_date$month, 
                             new_item[[1]]$start_date$day, sep = "-"))

    # Get a vector of dates from old_list
    dates_vector <- as.Date(vapply(old_list, function(x) paste(x$start_date$year, 
                                                         x$start_date$month, 
                                                         x$start_date$day, sep = "-"),
                             FUN.VALUE = character(1)))

    # Append the date from the new list item and sort
    dates_vector_new <- sort(c(dates_vector, new_item_date))

    # Get the position of the new list item date
    new_position <- which(dates_vector_new == new_item_date)

    # Append the new list item to the list
    if(new_position == 1){
      new_list <- append(new_item, old_list)
    } else {
      new_list <- append(old_list, new_item, after = new_position-1)
    }

    new_list
}

insert_new_list_item(old_list = my_list, new_item = new_list_item)

此问题还涵盖追加功能:How to insert an element in the middle of a list?