【问题标题】:change date values in 1 list to ith date value from another list将 1 个列表中的日期值更改为另一个列表中的第 i 个日期值
【发布时间】:2017-01-15 09:02:56
【问题描述】:

我有一系列日期

daysList <- seq(as.Date('2016-12-29'),as.Date('2017-01-01'),by="days")

countList,dayList 计数从 0 到最后一个值递减的列表:

countList <- list()
for (i in (length(daysList)-1):0) {
res <- 0:i
countList <- append(countList, list(res))
}

最后生成另一个列表,days_decremented,此列表将 daysList 中的日期值分配给 countList。

days_decrement <- lapply(rev(seq_along(daysList)), tail, x=daysList)

我需要生成另一个列表来更改 days_decrement 中的值以重复 daysList 中的第 i 个值。

所以结果类似于:

>daysCopy
[[1]]
[1] "2016-12-29" "2016-12-29" "2016-12-29" "2016-12-29"
[[2]]
[1] "2016-12-30" "2016-12-30" "2016-12-30"
[[3]]
[1] "2016-12-31" "2016-12-31"
[[4]]
[1] "2017-01-01"

我尝试了过多的 lapply、rep_len,甚至尝试循环遍历主 days_decrement 循环,将 [[i]][i] 值分配给 daysList[i] 值,但我一无所获。

像往常一样,我认为我缺少一个 lapply 实现。

【问题讨论】:

  • 你的意思类似于lapply(days_decrement, function(i)replace(i, i != i[1], i[1]))?
  • hmm 也许,它应该为整个列表索引重复第 i 个元素,我正在尝试将您的代码放入一个循环中,但它返回 NULL,啊!我现在一定已经浏览了 50 个 lapply,list pages on google,循环代码如下 t
  • 哦,所以你想要一个嵌套列表,如果第一个元素是我上面的 lapply 创建的列表?
  • 是的,我认为是这样的。
  • 这个? lapply(days_decrement, function(i) lapply(seq(length(i)), function(j) replace(i, i != i[j], i[j])))

标签: r lapply


【解决方案1】:

根据您的输出,一种方法是将days_decrement 的所有日期替换为第一个日期,即

lapply(days_decrement, function(i)replace(i, i != i[1], i[1]))

#[[1]]
#[1] "2016-12-29" "2016-12-29" "2016-12-29" "2016-12-29"
#[[2]]
#[1] "2016-12-30" "2016-12-30" "2016-12-30"
#[[3]]
#[1] "2016-12-31" "2016-12-31"
#[[4]]
#[1] "2017-01-01"

但是,如果您只想直接从countListdaysList 执行此操作,那么,

 Map(rep, daysList, lapply(countList, length)) 
 #or similarly Map(rep, daysList, lapply(days_decrement, length))

#[[1]]
#[1] "2016-12-29" "2016-12-29" "2016-12-29" "2016-12-29"
#[[2]]
#[1] "2016-12-30" "2016-12-30" "2016-12-30"
#[[3]]
#[1] "2016-12-31" "2016-12-31"
#[[4]]
#[1] "2017-01-01"

【讨论】:

    【解决方案2】:

    对于此类工作,您可以尝试使用purrr 包和函数式编程。 map 函数和它的朋友“有点像”lapply 但它们在用法和语法方面表现得更加一致和强大,管道的语法非常简洁。

    # your input
    daysList <- seq(as.Date('2016-12-29'),as.Date('2017-01-01'),by="days")
    
    # using purrr for creating your two lists
    library(purrr)
    
    # creating another kind of countlist
    countList <- 1:length(daysList) %>% map(~.x:length(daysList))
    countList
    #> [[1]]
    #> [1] 1 2 3 4
    #> 
    #> [[2]]
    #> [1] 2 3 4
    #> 
    #> [[3]]
    #> [1] 3 4
    #> 
    #> [[4]]
    #> [1] 4
    
    days_decrement <- rerun(length(daysList), daysList) %>% 
      map2(countList, ~.x[.y])
    days_decrement
    #> [[1]]
    #> [1] "2016-12-29" "2016-12-30" "2016-12-31" "2017-01-01"
    #> 
    #> [[2]]
    #> [1] "2016-12-30" "2016-12-31" "2017-01-01"
    #> 
    #> [[3]]
    #> [1] "2016-12-31" "2017-01-01"
    #> 
    #> [[4]]
    #> [1] "2017-01-01"
    
    # creating the one you asked
    daysCopy <- map2(daysList, length(daysList):1, rep)
    daysCopy
    #> [[1]]
    #> [1] "2016-12-29" "2016-12-29" "2016-12-29" "2016-12-29"
    #> 
    #> [[2]]
    #> [1] "2016-12-30" "2016-12-30" "2016-12-30"
    #> 
    #> [[3]]
    #> [1] "2016-12-31" "2016-12-31"
    #> 
    #> [[4]]
    #> [1] "2017-01-01"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多