将 map_dbl 输出转换为 R tidyverse 中的 tibble [英] converting map_dbl output to tibble in R tidyverse
本文介绍了将 map_dbl 输出转换为 R tidyverse 中的 tibble的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 R 中处理大量数据框,我想找到具有最小和最大列数的数据框,并找出它们列名的差异.但是,我在将 map_dbl 结果转换为常规 tibble 时遇到了困难.
I work with a large number of dataframes in R and I want to find the dataframes with the minimum and the maximum number of columns and finding the difference in their column names. However, I got stuck in turning my map_dbl results to a regular tibble.
first_df = data.frame(matrix(rnorm(20), nrow=10))
second_df = data.frame(matrix(rnorm(20), nrow=4))
third_df = data.frame(matrix(rnorm(20), nrow=5))
library(dplyr)
library(purrr)
library(tibble)
library(tidyr)
# capturing all the data frames
mget(ls(pattern = "_df")) %>%
map_dbl(ncol) %>%
as_tibble()
# expected output
# first_df 2
# second_df 5
## Finding the difference in columns
diff <- setdiff(colnames(first_df), colnames(second_df ))
推荐答案
你可以:
library(tidyverse)
min_max <- mget(ls(pattern = "_df")) %>%
map_dbl(ncol) %>%
enframe() %>%
arrange(value) %>%
slice(1, n())
min_max
# A tibble: 2 x 2
# name value
# <chr> <dbl>
#1 first_df 2
#2 second_df 5
setdiff(names(get(min_max$name[2])), names(get(min_max$name[1])))
#[1] "X3" "X4" "X5"
这篇关于将 map_dbl 输出转换为 R tidyverse 中的 tibble的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文