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

标签 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 处,并推送第二个元素“向下”。如果 start_date 是 2014-12-31,那么我希望它在一开始就将其他所有内容“向下”推,如果它是 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[[1]]$text),这并不理想。非常感谢任何帮助。

最佳答案

如果您像这样定义 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?

关于r - 如何根据时间顺序在未命名列表的中间插入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34297919/

相关文章:

r - 字符串在分隔符 : R 处拆分并展开(向量)

r - 使用另一个引用数据框更改数据框中的变量类别

r - 使用 R 中的匹配子类计算两个国家之间的相关性

python - 在 Python 中按不同级别排序

android - 如何像在 gmail 中一样使用滑动手势从列表中删除项目

list - 循环列表的神秘方案程序

R: 无法在 Ubuntu 14.04 上安装 'rasclass' 包

r - ggplot2 scale_x_log10() 破坏/不适用于通过 stat_function() 绘制的函数

c# - Qt : Find object in list by property

python - 如何使用 Beautiful Soup 提取具有某些类属性的列表项?