0
下記のDataProfile
のようなデータフレームのサマリーテーブルを作成しようとしています。 考え方は、各列を行に変換し、count、nulls、not nulls、uniqueの変数を追加し、それらの変数の追加の突然変異を追加することです。データフレーム変数を転記し、[r]のヌル、一意のカウントを追加します。
これを行うには、より高速な方法があるようです。これを行う関数はありますか?あなたが欲しい変数ごとに「プロファイル」の種類に得るためにあなたのデータをフォーマット後、1回のパスでそれを行うことで、コードを統合することができますので、
#trying to write the functions within dplyr & magrittr framework
library(tidyverse)
mtcars[2,2] <- NA # Add a null to test completeness
#
total <- mtcars %>% summarise_all(funs(n())) %>% melt
nulls <- mtcars %>% summarise_all(funs(sum(is.na(.)))) %>% melt
filled <- mtcars %>% summarise_all(funs(sum(!is.na(.)))) %>% melt
uniques <- mtcars %>% summarise_all(funs(length(unique(.)))) %>% melt
mtcars %>% summarise_all(funs(n_distinct(.))) %>% melt
#Build a Data Frame from names of mtcars and add variables with mutate
DataProfile <- as.data.frame(names(mtcars))
DataProfile <- DataProfile %>% mutate(Total = total$value,
Nulls = nulls$value,
Filled = filled $value,
Complete = Filled/Total,
Cardinality = uniques$value,
Uniqueness = Cardinality/Total,
Distinctness = Cardinality/Filled)
DataProfile
#These are other attempts with Base R, but they are harder to read and don't play well with summarise_all
sapply(mtcars, function(x) length(unique(x[!is.na(x)]))) %>% melt
rapply(mtcars,function(x)length(unique(x))) %>% melt