英文:
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 <-
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: "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" ...
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.
"2023-05-31"
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: "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" ...
and the structure of file last dates is
List of 1 $ file_last_dates.rds: Date[1:1], format: "2022-12-31"
答案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 <- 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"
... you could Map
or purrr::map
:
## note the double brackets:
dates |> Map(f = \(li) tail(li[[1]], 1))
## $from
## [1] "2023-06-29"
##
## $to
## [1] "2023-07-09"
... or recursively apply a function:
dates |> rapply(f = \(li) tail(li, 1)) |> as.Date(origin = '1970/01/01')
## from to
## "2023-06-29" "2023-07-09"
(Mind that, for unsorted dates, tail
will return the last date while max
will return the latest.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论