在各种数据框中比较 R 中的列名
Comparing Column names in R across various data frames
我目前正在尝试在进行任何转换和计算之前比较列 类 和 R 中各种数据框的名称。
我的代码如下所示::
library(dplyr)
m1 <- mtcars
m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))
out <- cbind(sapply(m1, class), sapply(m2, class), sapply(m3, class))
如果有人可以解决存储在列表中的数据框的问题,那就太好了。我所有的数据帧目前都存储在一个列表中,以便于处理。
All.list <- list(m1,m2,m3)
我希望输出以矩阵形式显示,如数据帧 "out" 所示。 "out" 中的输出是不可取的,因为它不正确。我希望输出更多如下::
我认为最简单的方法是定义一个函数,然后使用lapply和dplyr的组合来获得你想要的结果。这是我的做法。
library(dplyr)
m1 <- mtcars
m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))
All.list <- list(m1,m2,m3)
##Define a function to get variable names and types
my_function <- function(data_frame){
require(dplyr)
x <- tibble(`var_name` = colnames(data_frame),
`var_type` = sapply(data_frame, class))
return(x)
}
target <- lapply(1:length(All.list),function(i)my_function(All.list[[i]]) %>%
mutate(element =i)) %>%
bind_rows() %>%
spread(element, var_type)
target
尝试使用 janitor 包中的 compare_df_cols()
:
library(janitor)
compare_df_cols(All.list)
#> column_name All.list_1 All.list_2 All.list_3
#> 1 am numeric numeric numeric
#> 2 carb numeric numeric numeric
#> 3 cyl numeric factor factor
#> 4 disp numeric numeric numeric
#> 5 drat numeric numeric numeric
#> 6 gear numeric numeric numeric
#> 7 hp numeric numeric numeric
#> 8 mpg numeric numeric numeric
#> 9 qsec numeric numeric numeric
#> 10 vs numeric numeric numeric
#> 11 wt numeric numeric numeric
#> 12 xxxx1 <NA> factor <NA>
#> 13 xxxx2 <NA> <NA> factor
它接受名为 data.frames 的列表 and/or,即 compare_df_cols(m1, m2, m3)
。
免责声明:我维护最近添加了此功能的看门程序包 - 将其发布在这里,因为它恰好解决了这个用例。
我目前正在尝试在进行任何转换和计算之前比较列 类 和 R 中各种数据框的名称。 我的代码如下所示::
library(dplyr)
m1 <- mtcars
m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))
out <- cbind(sapply(m1, class), sapply(m2, class), sapply(m3, class))
如果有人可以解决存储在列表中的数据框的问题,那就太好了。我所有的数据帧目前都存储在一个列表中,以便于处理。
All.list <- list(m1,m2,m3)
我希望输出以矩阵形式显示,如数据帧 "out" 所示。 "out" 中的输出是不可取的,因为它不正确。我希望输出更多如下::
我认为最简单的方法是定义一个函数,然后使用lapply和dplyr的组合来获得你想要的结果。这是我的做法。
library(dplyr)
m1 <- mtcars
m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))
All.list <- list(m1,m2,m3)
##Define a function to get variable names and types
my_function <- function(data_frame){
require(dplyr)
x <- tibble(`var_name` = colnames(data_frame),
`var_type` = sapply(data_frame, class))
return(x)
}
target <- lapply(1:length(All.list),function(i)my_function(All.list[[i]]) %>%
mutate(element =i)) %>%
bind_rows() %>%
spread(element, var_type)
target
尝试使用 janitor 包中的 compare_df_cols()
:
library(janitor)
compare_df_cols(All.list)
#> column_name All.list_1 All.list_2 All.list_3
#> 1 am numeric numeric numeric
#> 2 carb numeric numeric numeric
#> 3 cyl numeric factor factor
#> 4 disp numeric numeric numeric
#> 5 drat numeric numeric numeric
#> 6 gear numeric numeric numeric
#> 7 hp numeric numeric numeric
#> 8 mpg numeric numeric numeric
#> 9 qsec numeric numeric numeric
#> 10 vs numeric numeric numeric
#> 11 wt numeric numeric numeric
#> 12 xxxx1 <NA> factor <NA>
#> 13 xxxx2 <NA> <NA> factor
它接受名为 data.frames 的列表 and/or,即 compare_df_cols(m1, m2, m3)
。
免责声明:我维护最近添加了此功能的看门程序包 - 将其发布在这里,因为它恰好解决了这个用例。