I have a list within data frame on which I want to use purrr::map() to test whether there are any NULL elements and then get rid of them.
While I was able to do this using sapply, but map doesn't work. I read https://cran.r-project.org/web/packages/purrr/purrr.pdf, but I can't figure out what's I am missing.
Here's my sapply code --this works well:
P_Trans<- P_Trans[!sapply(P_Trans$Group,is.null),]
Here's are a few things I tried for purrr::map, but they don't work.
Here are the four things I tried:
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))
Can someone please correct my mistake, and let me know what I is that I am doing wrong with above four options?
Data:
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"))
P_Trans[!map_lgl(P_Trans$Group,is.null),]? - Natemapwhen I have alistofdata.framesI want to iterate over, usually for modeling. By defaultmapis trying to return alistand using a wrapper likemap-lglis just forcing the result intovectorform similar to whatmap(p_trans$Group, is.null) %>% unlistwould do, but still preserving names and what not :) Hope that helps,mapstill gives me fits sometimes and I go back to thexapply- Natepurrr::discard()? - hrbrmstr