我在数据框中有一个列表,我想用它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"))