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

时间:2016-11-05 17:51:24

标签: r dplyr sapply purrr

我在数据框中有一个列表,我想用purrr::map()来测试是否有NULL个元素然后去除它们。

虽然我能够使用sapply执行此操作,但地图不起作用。我读过https://cran.r-project.org/web/packages/purrr/purrr.pdf,但我无法弄清楚我错过了什么。

这是我的sapply代码 - 这很有效:

P_Trans<- P_Trans[!sapply(P_Trans$Group,is.null),] 

以下是我为purrr::map尝试的一些内容,但它们不起作用。

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

a)

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"))

1 个答案:

答案 0 :(得分:0)

使用所有解决方案:

  • 您正在遍历P_Trans的每一列(不是通过项目/行)
  • 这些列是原子向量,列表(或data.frame)有名称,原子向量没有名称。 names(P_Trans[[1]]) # NULL
  • 你打算返回一个列表,而不是data.frame,虽然它在
  • 之前崩溃了
  • a与c
  • 相同
  • b与d
  • 相同

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

  • Group不存在,此处没有任何内容告诉我们应该在当前项目中查找它,更不用说表了

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

  • 你循环遍历4个原子向量,每次寻找一个名为Group的元素,没有任何元素(即使是第四个),所以$ operator is invalid for atomic vectors

lmap可以帮助您遍历列作为P_Trans的长度一个子列表,但是方法也会崩溃,只有最后一项会有一个名为Group的项目({{1 }})。

{@ 1}}解决方案的names(P_Trans[4]) # "Group"等同于{@ 1},因为map旨在返回P_Trans[!map_lgl(P_Trans$Group,is.null),]的向量,就像map_lgl所做的那样:

获得所需内容的其他方法:

logical