我在数据框中有一个列表,我想用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"))
答案 0 :(得分:0)
使用所有解决方案:
P_Trans
的每一列(不是通过项目/行)names(P_Trans[[1]]) # NULL
a)P_Trans %>% purrr::map(.,~is.null(Group))
:
Group
不存在,此处没有任何内容告诉我们应该在当前项目中查找它,更不用说表了 b)P_Trans %>% purrr::map(.,~is.null(.$Group))
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