- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用时 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()
?
最佳答案
您可以使用(截至目前未记录的).null
任何 map*()
的参数函数告诉函数在遇到 NULL
时该怎么做值(value):
map_df(problemlist, 'location', .null = data_frame(lat = NA, lng = NA) )
# lat lng
# 1 NA NA
# 2 NA NA
# 3 41.49 -71.46
关于r - Purrr::map_df() 删除 NULL 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48428107/
我想出了以下使用 purrr 包在 R 中创建数据框的方法: map_df(list, `[`, c("name_1", "name_2")) 虽然我之前用过 list %>% map_df(~dat
所以我有一个文件夹,其中每个文件都是“几乎”相同的 CSV 文件。他们每个看起来都像: EMP ID WORK DATE WORK HOURS JOB TITLE MGMT CTR 0
我以 mtcars 数据集为例来说明我的问题。我对每种圆柱类型进行了线性回归,并使用 map_df 将所有模型结果放在一起。 (代码和输出如下)。我想要做的是添加另一列名为“圆柱体”(4,4,6,6,
我正在寻找相当于 next 在循环中的 purrr::map_df 调用。 map_df 可以很好地处理 NULL 的数据帧(如下例所示),所以当我设置 Result % filter(y == ma
使用时 purrr::map_df() ,我偶尔会传入一个数据框列表,其中有些项目是 NULL .当我这样做时,map_df()返回行数少于原始列表的数据框。 我假设正在发生的事情是 map_df()
我正在寻找替换我的一些使用 dplyr::do 的 R 代码,因为这个函数很快就会被弃用。我的很多工作都需要创建分层 CDF 图。使用 dply:do 时,我分层的变量作为变量传递给结果数据框,然后我
我正在使用 spotifyr 包为我的数据集中特定专辑的每首歌曲抓取 Spotify 音频功能。我的问题是我的数据集包含一些不在 spotify 上的艺术家——所以他们不应该返回任何值。 我的问题是,
我是一名优秀的程序员,十分优秀!