当前位置: 首页 > 工具软件 > Purrr > 使用案例 >

java map 递归_使用purrr :: map进行递归函数调用

权承
2023-12-01

我有一个数据框中的列表,我想使用 purrr::map() 测试是否有任何 NULL 元素,然后摆脱它们 .

虽然我能够使用sapply来做到这一点,但是 Map 不起作用 . 我读了https://cran.r-project.org/web/packages/purrr/purrr.pdf,但我可以't figure out what'我错过了 .

这是我的sapply代码 - 这很好用:

P_Trans

这是我为 purrr::map 尝试的一些事情,但它们不起作用 .

以下是我尝试过的四件事:

一个)

P_Trans %>% purrr::map(.,~is.null(Group))

b)

P_Trans %>% purrr::map(.,~is.null(.$Group))

C)

P_Trans %>% purrr::map(~is.null(.$Group))

d)

P_Trans %>% purrr::map(~is.null(Group))

有人可以纠正我的错误,让我知道我在做什么,我做错了以上四个选项?

数据:

dput(P_Trans)

structure(list(TransactionID = c("a1", "a1", "a1", "a2", "a2",

"a2", "a3", "a3", "a3", "a3", "a4", "a5", "a5", "a5", "a5", "a5",

"a6", "a6", "a7"), ProductID = c("A", "B", "1", "C", "4", "5",

"D", "C", "7", "8", "H", "1", "2", "3", "3", "1", "H", "15",

"22"), ProductType = c(1, 1, 2, 1, 2, 2, 1, 1, 2, 2, 1, 2, 2,

2, 2, 2, 1, 2, 3), Group = list(structure(list(Group = "Group1"), .Names = "Group", row.names = c(NA,

-1L), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group1"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group1"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = c("Group2", "Group3")), .Names = "Group", row.names = c(NA,

-2L), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group2"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group2"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group3"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = c("Group2", "Group3")), .Names = "Group", row.names = c(NA,

-2L), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group3"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group3"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group5"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group1"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group1"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group1"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group1"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group1"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group5"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), structure(list(

Group = "Group5"), .Names = "Group", row.names = c(NA, -1L

), class = c("tbl_df", "tbl", "data.frame")), NULL)), class = c("tbl_df",

"tbl", "data.frame"), row.names = c(NA, -19L), .Names = c("TransactionID",

"ProductID", "ProductType", "Group"))

 类似资料: