3

我在数据框中有一个列表,我想用它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,但它们不起作用。

以下是我尝试的四件事:

一个)

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

1 回答 1

0

使用您的所有解决方案:

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

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

  • Group不存在,这里没有告诉我们应该在当前项目中寻找它,更不用说在表中

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

  • 您正在循环 4 个原子向量,每次寻找一个名为 的元素时Group,都没有(甚至是第 4 个),所以$ operator is invalid for atomic vectors

lmap本来可以帮助您将列作为长度的一个子列表进行循环,P_Trans但方法也会崩溃,只有最后一个项目会有一个名为Group( names(P_Trans[4]) # "Group") 的项目。

您的map解决方案等效P_Trans[!map_lgl(P_Trans$Group,is.null),]于@Nate 评论,因为map_lgl它旨在返回logical像您sapply所做的那样的向量:

获得您想要的东西的其他方法:

P_Trans %>% rowwise %>% filter(!is.null(Group))
P_Trans %>% filter(lengths(Group)!=0)
P_Trans[lengths(P_Trans$Group)!=0,]
于 2017-10-26T22:22:47.197 回答