2つの異なるサイズのデータセットをマージし、同じ行を小さいデータセットから大きいデータセットの複数の行に条件付きでコピーします

ハンガリー語

私はRにまったく慣れておらず、しばらくの間、問題の代表的な解決策をグーグルで調べてみましたが、今のところ適切な答えが見つからないので、助けを求めることでこれを解決できることを願っています。

2つの異なるサイズのデータ​​セットをマージする必要があります(その他には年次データ:df_f、その他の月次データ:df_mが含まれます)。df_fの行が条件付きでdf_mとマージされるように、小さい方のdf_fを大きい方のdf_mにマージする必要があります。

これが私の問題の説明的な例です(いくつかの非常に基本的な再現可能な数値を使用):

最初のデータセット

a <- c(1990)
b <- c(1980:1981)
c <- c(1994:1995)

aa <- rep("A", 1) 
bb <- rep("B", 2)
cc <- rep("C", 2)

df1 <- data.frame(comp=factor(c(aa, bb, cc))) 
df2 <- data.frame(year=factor(c(a, b, c))) 
other.columns <- rep("other_columns", length(df1))

df_f <- cbind(df1, df2, other.columns ) # first dataset

2番目のデータセット

z <- c(10:12)
x <- c(7:12)
xx <- c(1:9)
v <- c(2:9)

w <- rep(1990, length(z))
e <- rep(1980, length(x))
ee <- rep (1981, length(xx))
r <- rep(1995, length(v))

t <- rep("A", length(z))
y <- rep("B", length(x) + length(xx))
u <- rep("C", length(v))

df3 <- data.frame(month=factor(c(z, x, xx, v))) 
df4 <- data.frame(year=factor(c(w, e, ee, r))) 
df5 <- data.frame(comp=factor(c(t, y, u))) 

df_m <- cbind(df5, df4, df3) # second dataset 

出力:

> df_m
   comp year month
1     A 1990    10
2     A 1990    11
3     A 1990    12
4     B 1980     7
5     B 1980     8
6     B 1980     9
7     B 1980    10
8     B 1980    11
9     B 1980    12
10    B 1981     1
11    B 1981     2
12    B 1981     3
13    B 1981     4
14    B 1981     5
15    B 1981     6
16    B 1981     7
17    B 1981     8
18    B 1981     9
19    C 1995     2
20    C 1995     3
21    C 1995     4
22    C 1995     5
23    C 1995     6
24    C 1995     7
25    C 1995     8
26    C 1995     9
> df_f
  comp year other.columns
1    A 1990 other_columns
2    B 1980 other_columns
3    B 1981 other_columns
4    C 1994 other_columns
5    C 1995 other_columns

comp、year、monthの条件に従って、df_fからdf_mに行を配置します(df_fからdf_mの新しい列にデータを格納します)。Comp(会社)は常に一致する必要がありますが、年の一致は月を条件とします。月が6を超える場合、年はデータセット間で一致し、月が7未満の場合、年+ 1(df_m)は年(df_f)と一致します。 )。df_fの特定の行は、条件に応じてdf_mの複数の行に配置する必要があることに注意してください。

必要な出力により、問題と目標が明確になります。

必要な出力:

    comp year month comp year other.columns
1     A 1990    10    A 1990 other_columns
2     A 1990    11    A 1990 other_columns
3     A 1990    12    A 1990 other_columns
4     B 1980     7    B 1980 other_columns
5     B 1980     8    B 1980 other_columns
6     B 1980     9    B 1980 other_columns
7     B 1980    10    B 1980 other_columns
8     B 1980    11    B 1980 other_columns
9     B 1980    12    B 1980 other_columns
10    B 1981     1    B 1980 other_columns
11    B 1981     2    B 1980 other_columns
12    B 1981     3    B 1980 other_columns
13    B 1981     4    B 1980 other_columns
14    B 1981     5    B 1980 other_columns
15    B 1981     6    B 1980 other_columns
16    B 1981     7    B 1981 other_columns
17    B 1981     8    B 1981 other_columns
18    B 1981     9    B 1981 other_columns
19    C 1995     2    C 1994 other_columns
20    C 1995     3    C 1994 other_columns
21    C 1995     4    C 1994 other_columns
22    C 1995     5    C 1994 other_columns
23    C 1995     6    C 1994 other_columns
24    C 1995     7    C 1995 other_columns   
25    C 1995     8    C 1995 other_columns
26    C 1995     9    C 1995 other_columns

事前にどうもありがとうございました!質問が十分に明確であることを願っています、少なくともそれを説明するのは少し難しかったです。

Stibu

問題を解決するための基本的な考え方は、照合に使用する必要のある年を列に追加することです。dpylrこのパッケージと他の操作手順にパッケージ使用します。

テーブルを組み合わせる前に、数値列を数値に変換する必要があります。

library(dplyr)
df_m <- mutate(df_m, year = as.numeric(as.character(year)),
                     month = as.numeric(as.character(month)))
df_f <- mutate(df_f, year = as.numeric(as.character(year)))

その理由は、月(month > 6)と数値比較を行い、年から1を引くことができるようにするためですファクターでこれを行うことはできません。

次に、マッチングに使用する列を追加します。

df_m <- mutate(df_m, match_year = ifelse(month >= 7, year, year - 1))

そして最後のステップで、2つのテーブルを結合します。

df_new <- left_join(df_m, df_f, by = c("comp", "match_year" = "year"))

引数byは、2つのデータフレームのどの列を一致させるかを決定します。出力は結果と一致します。

##    comp year month match_year other.columns
## 1     A 1990    10       1990 other_columns
## 2     A 1990    11       1990 other_columns
## 3     A 1990    12       1990 other_columns
## 4     B 1980     7       1980 other_columns
## 5     B 1980     8       1980 other_columns
## 6     B 1980     9       1980 other_columns
## 7     B 1980    10       1980 other_columns
## 8     B 1980    11       1980 other_columns
## 9     B 1980    12       1980 other_columns
## 10    B 1981     1       1980 other_columns
## 11    B 1981     2       1980 other_columns
## 12    B 1981     3       1980 other_columns
## 13    B 1981     4       1980 other_columns
## 14    B 1981     5       1980 other_columns
## 15    B 1981     6       1980 other_columns
## 16    B 1981     7       1981 other_columns
## 17    B 1981     8       1981 other_columns
## 18    B 1981     9       1981 other_columns
## 19    C 1995     2       1994 other_columns
## 20    C 1995     3       1994 other_columns
## 21    C 1995     4       1994 other_columns
## 22    C 1995     5       1994 other_columns
## 23    C 1995     6       1994 other_columns
## 24    C 1995     7       1995 other_columns
## 25    C 1995     8       1995 other_columns
## 26    C 1995     9       1995 other_columns

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

TOP 一覧

  1. 1

    グラフからテーブルに条件付き書式を適用するにはどうすればよいですか?

  2. 2

    ソートされた検索、ターゲット値未満の数をカウント

  3. 3

    Unity:未知のスクリプトをGameObject(カスタムエディター)に動的にアタッチする方法

  4. 4

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  5. 5

    Ansibleで複数行のシェルスクリプトを実行する方法

  6. 6

    Reactでclsxを使用する方法

  7. 7

    tkinterウィンドウを閉じてもPythonプログラムが終了しない

  8. 8

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  9. 9

    Pythonを使用して同じ列の同じ値の間の時差を取得する方法

  10. 10

    PowerShellの分割ファイルへのヘッダーの追加

  11. 11

    Chromeウェブアプリのウェブビューの高さの問題

  12. 12

    BLOBストレージからデータを読み取り、Azure関数アプリを使用してデータにアクセスする方法

  13. 13

    Crashlytics:コンパイラー生成とはどういう意味ですか?

  14. 14

    GoDaddyでのCKEditorとKCfinderの画像プレビュー

  15. 15

    Windows 10の起動時間:以前は20秒でしたが、現在は6〜8倍になっています

  16. 16

    MLでのデータ前処理の背後にある直感

  17. 17

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  18. 18

    reCAPTCHA-エラーコード:ユーザーの応答を検証するときの「missing-input-response」、「missing-input-secret」(POSTの詳細がない)

  19. 19

    STSでループプロセス「クラスパス通知の送信」のループを停止する方法

  20. 20

    ファイル内の2つのマーカー間のテキストを、別のファイルのテキストのセクションに置き換えるにはどうすればよいですか?

  21. 21

    ネットワークグラフで、ネットワークコンポーネントにカーソルを合わせたときに、それらを強調表示するにはどうすればよいですか?

ホットタグ

アーカイブ