r - Remove columns from data.frame that are of type list
问题描述
Given a data.frame that has list columns and trying to write it to a csv file, how can a user drop all columns of type list?
dput would be quite long. See an example here Note that full df has 5+ list columns and I prefer not enumerating them or hunting them by name.
> str(df,max.level=1)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 2237 obs. of 30 variables:
$ CATEGORY : chr "ARTICLE " "ARTICLE " "ARTICLE " "ARTICLE " ...
$ BIBTEXKEY : chr "RN69" "RN4023" "RN3332" "RN58" ...
$ ADDRESS : chr NA NA NA NA ...
$ ANNOTE : chr NA NA NA NA ...
$ AUTHOR :List of 2237
$ BOOKTITLE : chr NA NA NA NA ...
and 40+ other columns
> names(df)
[1] "CATEGORY" "BIBTEXKEY" "ADDRESS" "ANNOTE" "AUTHOR" "BOOKTITLE"
[7] "CHAPTER" "CROSSREF" "EDITION" "EDITOR" "HOWPUBLISHED" "INSTITUTION"
[13] "JOURNAL" "KEY" "MONTH" "NOTE" "NUMBER" "ORGANIZATION"
[19] "PAGES" "PUBLISHER" "SCHOOL" "SERIES" "TITLE" "TYPE"
[25] "VOLUME" "YEAR" "ISSN" "DOI" "ISBN" "URL"
>
the command should looke something like
df %>% select_if(!is.list)
but it is not fully correct
df comes from
devtools::install_github("ropensci/bib2df")
library(bib2df)
url <- "https://cprd.com/bibliography/export/bibtex"
df <- bib2df(url)
this selects them correctly but negation seems hard to do
df %>% select_if(is_list)
解决方案
给定
dat <- tibble::tibble(a = 1,
b = list(d = c(1, 2)))
我们可以用
Filter(Negate(is.list), dat)
要得到
# A tibble: 1 x 1
# a
# <dbl>
#1 1
输入Negate
控制台,我们会看到它的作用
function (f)
{
f <- match.fun(f)
function(...) !f(...)
}
推荐阅读
- console - orientdb集群/控制台
- spring-boot - 如何在执行器prometheus中导入执行器httptrace?(执行器、弹簧靴、grafana)
- python - Microbike,cmd 错误:没有名为“microbit”的模块
- python - Django - 图像不会在我的个人资料页面中上传和更改
- c++ - 使用 gdb 和 -D_GLIBCXX_DEBUG
- flutter - 每次在 Flutter 中调用 Widget build(BuildContext context) 时如何添加动画
- rust - ioctl 中应该使用哪个文件描述符来了解终端屏幕大小?
- 2d - 在 Godot 中,如果 vector.y 已达到最大值,如何限制 vector.x?
- mysql - 使用 MariaDB 引擎 ColumnStore 的慢速 CREATE TABLE
- javascript - Leaflet 使用 jquery 切换标记的 groupLayer