本文へスキップ
からだにいいもの

Rのトピックスを中心に『まだ、まだ、知らない、役に立つ情報?』を発信します。

Rで解析:2つのデータの差異を確認「versus」パッケージ

2つのデータの差異を確認するのに便利なパッケージの紹介です。ただ、差異キー列データはユニークなデータであることが必要なので注意です。

パッケージバージョンは0.3.0。実行コマンドはwindows 11のR version 4.3.3で確認しています。

パッケージのインストール

下記コマンドを実行してください。

# パッケージのインストール
install.packages("versus")
スポンサーリンク

コマンドの紹介

詳細はコマンド、パッケージのヘルプを確認してください。

なお、紹介ではfactor classをcharacter classに変換していますが、変換しなくとも動作します。

# パッケージの読み込み
library("versus")

### データ例の準備 #####
# tidyverseパッケージの読み込み
# tidyverseパッケージがなければインストール
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}

set.seed(1234)
Test_A <- as_tibble(iris[sample(nrow(iris), 10),]) |>
  mutate(Species = as.character(Species)) |>
  distinct(Species, .keep_all = TRUE)

Test_A
# A tibble: 3 × 5
#    Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
#           <dbl>       <dbl>        <dbl>       <dbl> <chr>     
#  1          5.2         3.5          1.5         0.2 setosa    
#  2          5.7         2.6          3.5         1   versicolor
#  3          6.3         3.3          6           2.5 virginica 

set.seed(4567)
Test_B <- as_tibble(iris[sample(nrow(iris), 10),]) |>
  mutate(Species = as.character(Species)) |>
  distinct(Species, .keep_all = TRUE) |>
  rename("Width" = Sepal.Width)

Test_B[2, 5] <- "virginica_02"

Test_B
# A tibble: 3 × 5
#    Sepal.Length Width Petal.Length Petal.Width Species     
#           <dbl> <dbl>        <dbl>       <dbl> <chr>       
#  1          4.7   3.2          1.6         0.2 setosa      
#  2          7.4   2.8          6.1         1.9 virginica_02
#  3          5.9   3.2          4.8         1.8 versicolor  
########

# データの差異情報を表示:compareコマンド
# 比較基準になる列を指定:byオプション;両方のデータ共にUniqueデータ
compare(table_a = Test_A, table_b = Test_B, by = Species)
# $tables
# # A tibble: 2 × 4
#   table   expr    nrow  ncol
#   <chr>   <chr>  <int> <int>
# 1 table_a Test_A     3     5
# 2 table_b Test_B     3     5
# 
# $by
# # A tibble: 1 × 3
#   column  class_a   class_b  
#   <chr>   <chr>     <chr>    
# 1 Species character character
# 
# $intersection
# # A tibble: 3 × 5
#     column       n_diffs class_a class_b diff_rows       
#     <chr>          <int> <chr>   <chr>   <list>          
#   1 Sepal.Length       2 numeric numeric <tibble [2 × 2]>
#   2 Petal.Length       2 numeric numeric <tibble [2 × 2]>
#   3 Petal.Width        1 numeric numeric <tibble [1 × 2]>
#   
# $unmatched_cols
# # A tibble: 2 × 2
#   table column     
#   <chr> <chr>      
# 1 a     Sepal.Width
# 2 b     Width      
# 
# $unmatched_rows
# # A tibble: 2 × 3
#   table Species        row
#   <chr> <chr>        <int>
# 1 a     virginica        3
# 2 b     virginica_02     2

# データの指定列の差異の値を表示:value_diffsコマンド
# 注:compareコマンドの結果を使用します
# compareコマンドの結果を設定:comparisonオプション
# 列を指定:comparisonオプション
compare(table_a = Test_A, table_b = Test_B, by = Species) |>
  value_diffs(Sepal.Length)
# A tibble: 2 × 3
#  Sepal.Length_a Sepal.Length_b Species   
#       <dbl>          <dbl> <chr>     
# 1            5.2            4.7 setosa    
# 2            5.7            5.9 versicolor

# データの差異の全値を表示:value_diffs_stackedコマンド
compare(table_a = Test_A, table_b = Test_B, by = Species) |>
  value_diffs_stacked()
# A tibble: 5 × 4
#  column       val_a val_b Species   
#  <chr>        <dbl> <dbl> <chr>     
# 1 Sepal.Length   5.2   4.7 setosa    
# 2 Sepal.Length   5.7   5.9 versicolor
# 3 Petal.Length   1.5   1.6 setosa    
# 4 Petal.Length   3.5   4.8 versicolor
# 5 Petal.Width    1     1.8 versicolor

# 指定したデータの差異のデータを取得:slice_diffsコマンド
# 取得するデータを指定:tableオプション;"a"/"b"で指定 
compare(table_a = Test_A, table_b = Test_B, by = Species) |>
  slice_diffs(table = "a", column = Sepal.Length)
# A tibble: 2 × 5
#     Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
#            <dbl>       <dbl>        <dbl>       <dbl> <chr>     
#   1          5.2         3.5          1.5         0.2 setosa    
#  2          5.7         2.6          3.5         1   versicolor

少しでも、あなたの解析が楽になりますように!!

スポンサーリンク
Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.