Purrr::map_df() 删除 NULL 行
Purrr::map_df() drops NULL rows
使用purrr::map_df()
时,我偶尔会传入一个数据框列表,其中有些项目是NULL
。当我这样做时,map_df()
returns 一个数据框的行数比原始列表少。
我假设发生的事情是 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()
),但这并不是我 运行陷入错误——我只是没有得到想要的结果。
用 map_df()
处理 NULL
值的最佳方法是什么?
您可以对任何 map*()
函数使用(截至目前未记录的).null
参数来告诉函数遇到 NULL
值时该做什么:
map_df(problemlist, 'location', .null = data_frame(lat = NA, lng = NA) )
# lat lng
# 1 NA NA
# 2 NA NA
# 3 41.49 -71.46
使用purrr::map_df()
时,我偶尔会传入一个数据框列表,其中有些项目是NULL
。当我这样做时,map_df()
returns 一个数据框的行数比原始列表少。
我假设发生的事情是 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()
),但这并不是我 运行陷入错误——我只是没有得到想要的结果。
用 map_df()
处理 NULL
值的最佳方法是什么?
您可以对任何 map*()
函数使用(截至目前未记录的).null
参数来告诉函数遇到 NULL
值时该做什么:
map_df(problemlist, 'location', .null = data_frame(lat = NA, lng = NA) )
# lat lng
# 1 NA NA
# 2 NA NA
# 3 41.49 -71.46