Purrr :: map_df()删除NULL行

时间:2018-01-24 17:12:12

标签: r tidyverse purrr

使用purrr::map_df()时,我偶尔会传入一些数据框列表,其中某些项目为NULL。当我这样做时,map_df()返回的行数比原始列表少。

我假设发生了什么map_df()调用dplyr::bind_rows()忽略NULL值。但是,我不确定如何识别有问题的行。

以下是一个例子:

library(purrr)

problemlist  <- list(NULL, NULL, structure(list(bounds = structure(list(northeast = structure(list(
    lat = 41.49, lng = -71.46), .Names = c("lat", "lng"
), class = "data.frame", row.names = 1L), southwest = structure(list(
    lat = 41.49, lng = -71.46), .Names = c("lat", "lng"
), class = "data.frame", row.names = 1L)), .Names = c("northeast", 
"southwest"), class = "data.frame", row.names = 1L), location = structure(list(
    lat = 41.49, lng = -71.46), .Names = c("lat", "lng"
), class = "data.frame", row.names = 1L), location_type = "ROOFTOP", 
    viewport = structure(list(northeast = structure(list(lat = 41.49, 
        lng = -71.46), .Names = c("lat", "lng"), class = "data.frame", row.names = 1L), 
        southwest = structure(list(lat = 41.49, lng = -71.46), .Names = c("lat", 
        "lng"), class = "data.frame", row.names = 1L)), .Names = c("northeast", 
    "southwest"), class = "data.frame", row.names = 1L)), .Names = c("bounds", 
"location", "location_type", "viewport"), class = "data.frame", row.names = 1L))

# what actually happens
map_df(problemlist, 'location')

#     lat    lng
# 1 41.49 -71.46


# desired result
map_df_with_Null_handling(problemlist, 'location') 

#     lat    lng
# 1    NA     NA
# 2    NA     NA
# 3 41.49 -71.46

我考虑将我的location访问器包装在purrr的一个错误处理函数中(例如safely()possibly()),但这不是我遇到错误 - 我我只是没有得到预期的结果。

使用NULL处理map_df()值的最佳方式是什么?

1 个答案:

答案 0 :(得分:4)

您可以对任何.null函数使用(as-of-present notocumented)map*()参数来告诉函数遇到NULL值时要执行的操作:< / p>

map_df(problemlist, 'location', .null = data_frame(lat = NA, lng = NA) )

#     lat    lng
# 1    NA     NA
# 2    NA     NA
# 3 41.49 -71.46