如何将函数列表的输出进行索引以在另一个函数中使用?

huangapple go评论79阅读模式
英文:

How to index a function list output for use in another function?

问题

我正在使用lapply()来处理一个包含2个文件的列表,并使用compose()来应用多个函数。正在应用的两个函数将加载文件中的数据(2个日期列表的列表),然后选择列表中的最后一个元素。

问题出现在当我尝试选择每个日期列表中的最后一个元素时。与其说是tail()函数输出一个日期,它输出整个列表。我发现我可以通过data[[1]]来索引加载的数据,对于第一个列表,通过data[[2]]来索引第二个列表,以获取正确的最终元素。我不知道如何在lapply()、compose()和partial()的上下文中执行这个操作。

以下是原始代码:

dates <- 
  list(
    from = lapply(file_last_dates,
                  purrr::compose(as.Date, purrr::partial(sum,1))),
    up_to = lapply(tamsat_retrieved_dates,
                   purrr::compose(purrr::partial(tail,n=1), st_load_rds))
    )

它输出以下结构:

List of 2
 $ from :List of 1
  ..$ file_last_dates.rds: Date[1:1], format: "2023-01-01"
 $ up_to:List of 2
  ..$ :List of 1
  .. ..$ ST/rfe/raw/ssa/tamsat-v3.1/dekadal/retrieved_dates.rds: Date[1:1419], format: "1984-01-10" "1984-01-20" "1984-01-31" "1984-02-10" ...
  ..$ :List of 1
  .. ..$ ST/rfe/raw/ssa/tamsat-v3.1/daily/retrieved_dates.rds: Date[1:14396], format: "1984-01-01" "1984-01-02" "1984-01-03" "1984-01-04" ...

另一方面,以下代码:

tail(st_load_rds(tamsat_retrieved_dates)[[1]],n=1)

tail(st_load_rds(tamsat_retrieved_dates)[[2]],n=1)

给我想要的输出,如下所示:

"2023-05-31"

st_load_rds(tamsat_retrieved_dates)的结构如下:

List of 2 $ ST/rfe/raw/ssa/tamsat-v3.1/dekadal/retrieved_dates.rds: Date[1:1419], format: "1984-01-10" "1984-01-20" "1984-01-31" "1984-02-10" ... $ ST/rfe/raw/ssa/tamsat-v3.1/daily/retrieved_dates.rds : Date[1:14396], format: "1984-01-01" "1984-01-02" "1984-01-03" "1984-01-04" ...

而file_last_dates的结构如下:

List of 1 $ file_last_dates.rds: Date[1:1], format: "2022-12-31"
英文:

I am using lapply() to take in a list of 2 files and compose() to apply multiple functions. The two functions being applied will load in the data from the file (list of 2 lists of dates) and then select the last element in the list.

The issue arises when I try to select the last element in each list of dates. Rather than the tail() function outputting one date, it outputs the whole list. I find that I can index the loaded data by data[[1]] for the first list and data[[2]] for the second one to get the correct final element. I don't know how to do this within the context of lapply(), compose(), and partial().

Here is the original code:

dates &lt;- 
  list(
    from = lapply(file_last_dates,
                  purrr::compose(as.Date, purrr::partial(sum,1))),
    up_to = lapply(tamsat_retrieved_dates,
                   purrr::compose(purrr::partial(tail,n=1), st_load_rds))
    )

which outputs the following structure.

List of 2
 $ from :List of 1
  ..$ file_last_dates.rds: Date[1:1], format: &quot;2023-01-01&quot;
 $ up_to:List of 2
  ..$ :List of 1
  .. ..$ ST/rfe/raw/ssa/tamsat-v3.1/dekadal/retrieved_dates.rds: Date[1:1419], format: &quot;1984-01-10&quot; &quot;1984-01-20&quot; &quot;1984-01-31&quot; &quot;1984-02-10&quot; ...
  ..$ :List of 1
  .. ..$ ST/rfe/raw/ssa/tamsat-v3.1/daily/retrieved_dates.rds: Date[1:14396], format: &quot;1984-01-01&quot; &quot;1984-01-02&quot; &quot;1984-01-03&quot; &quot;1984-01-04&quot; ...

On the other hand, the following code

tail(st_load_rds(tamsat_retrieved_dates)[[1]],n=1)

and

tail(st_load_rds(tamsat_retrieved_dates)[[2]],n=1)

give me the output I want, below.

&quot;2023-05-31&quot;

The structure of st_load_rds(tamsat_retrieved_dates) is

List of 2 $ ST/rfe/raw/ssa/tamsat-v3.1/dekadal/retrieved_dates.rds: Date[1:1419], format: &quot;1984-01-10&quot; &quot;1984-01-20&quot; &quot;1984-01-31&quot; &quot;1984-02-10&quot; ... $ ST/rfe/raw/ssa/tamsat-v3.1/daily/retrieved_dates.rds : Date[1:14396], format: &quot;1984-01-01&quot; &quot;1984-01-02&quot; &quot;1984-01-03&quot; &quot;1984-01-04&quot; ...

and the structure of file last dates is

List of 1 $ file_last_dates.rds: Date[1:1], format: &quot;2022-12-31&quot;

答案1

得分: 1

有这样的结构...

dates <- list(from = list(Sys.Date() + 1:3),
to = list(Sys.Date() + 10:13)
)

$from
$from[[1]]
[1] "2023-06-27" "2023-06-28" "2023-06-29"

$to
$to[[1]]
[1] "2023-07-06" "2023-07-07" "2023-07-08" "2023-07-09"

... 你可以使用 `Map` 或 `purrr::map`:

注意双括号:

dates |> Map(f = (li) tail(li[[1]], 1))

$from

[1] "2023-06-29"

$to

[1] "2023-07-09"

... 或者递归应用一个函数:

dates |> rapply(f = (li) tail(li, 1)) |> as.Date(origin = '1970/01/01')

from to

"2023-06-29" "2023-07-09"


(请注意,对于未排序的日期,`tail` 将返回最后日期,而 `max` 将返回最新日期。)
英文:

With a structure like this ...

dates &lt;- list(from = list(Sys.Date() + 1:3),
              to = list(Sys.Date() + 10:13)
              )
$from
$from[[1]]
[1] &quot;2023-06-27&quot; &quot;2023-06-28&quot; &quot;2023-06-29&quot;


$to
$to[[1]]
[1] &quot;2023-07-06&quot; &quot;2023-07-07&quot; &quot;2023-07-08&quot; &quot;2023-07-09&quot;

... you could Map or purrr::map:

## note the double brackets:
dates |&gt; Map(f = \(li) tail(li[[1]], 1))
## $from
## [1] &quot;2023-06-29&quot;
## 
## $to
## [1] &quot;2023-07-09&quot;

... or recursively apply a function:

dates |&gt; rapply(f = \(li) tail(li, 1)) |&gt; as.Date(origin = &#39;1970/01/01&#39;)
##         from           to 
## &quot;2023-06-29&quot; &quot;2023-07-09&quot; 

(Mind that, for unsorted dates, tail will return the last date while max will return the latest.)

huangapple
  • 本文由 发表于 2023年6月26日 16:03:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定