2016-12-18 35 views
6

ネットワーク視覚化手法のテストに興味がありますが、これらの関数を試す前に、次のようなデータフレームを使用して隣接行列を作成します。rデータフレーム内の列から隣接行列を作成する

Id Gender Col_Cold_1 Col_Cold_2 Col_Cold_3 Col_Hot_1 Col_Hot_2 Col_Hot_3 
10 F   pain  sleep  NA   infection medication walking 
14 F   Bump  NA   muscle  NA   twitching flutter 
17 M     pain   hemoloma Callus  infection 
18 F   muscle     pain     twitching medication 

私の目標は、例えば

1) All values in columns with keyword Cold will contribute to the rows 
2) All values in columns with keyword Hot will contribute to the columns 

を次のように隣接行列を作成することで、pain, sleep, Bump, muscle, hemalomaは、キーワードと列の下にコールドセル値であり、彼らは、以下のような行やセルの値を形成することになりますinfection, medication, Callus, walking, twitching, flutterはキーワードのホットを持つ列の下にあり、これは関連マトリックスの列を形成します。

最終的な所望の出力は次のように表示されます:

  infection medication walking twitching flutter Callus 
    pain 2   2   1  1     1 
    sleep 1   1   1 
    Bump         1   1 
    muscle    1     1 
hemaloma 1             1 
  • [pain, infection] = 2疼痛および感染の間の関連は、元のデータフレームに二回発生するため:行3に再び行1と。

  • [pain, medication] = 2疼痛及び薬物間の関連付けは行1で二回1回発生し、再度行のため4.

このような関連マトリックスを作成するためのアドバイスやアドバイスをいただきありがとうございます。

再現データセット

df = structure(list(id = c(10, 14, 17, 18), Gender = structure(c(1L, 1L, 2L, 1L), .Label = c("F", "M"), class = "factor"), Col_Cold_1 = structure(c(4L, 2L, 1L, 3L), .Label = c("", "Bump", "muscle", "pain"), class = "factor"), Col_Cold_2 = structure(c(4L, 2L, 3L, 1L), .Label = c("", "NA", "pain", "sleep"), class = "factor"), Col_Cold_3 = structure(c(1L, 3L, 2L, 4L), .Label = c("NA", "hemaloma", "muscle", "pain"), class = "factor"), Col_Hot_1 = structure(c(4L, 3L, 2L, 1L), .Label = c("", "Callus", "NA", "infection"), class = "factor"), Col_Hot_2 = structure(c(2L, 3L, 1L, 3L), .Label = c("infection", "medication", "twitching"), class = "factor"), Col_Hot_3 = structure(c(4L, 2L, 1L, 3L), .Label = c("", "flutter", "medication", "walking"), class = "factor")), .Names = c("id", "Gender", "Col_Cold_1", "Col_Cold_2", "Col_Cold_3", "Col_Hot_1", "Col_Hot_2", "Col_Hot_3"), row.names = c(NA, -4L), class = "data.frame") 
+1

すでに隣接行列を作成する上での情報のトンがあります:[1](http://stackoverflow.com

out # cold hot # 1 pain infection # 2 Bump <NA> # 3 <NA> Callus # 4 muscle <NA> # 5 pain medication # ... 

最後に、所望の出力を行うためにxtabsを使用します/ a/14850986/1152809)、[two](https://www.r-bloggers.com/graph-from-sparse-adjacency-matrix/)、[three(pdf)](https://www.google .COM/URL?SA = T&RCT = J&Q =&ESRC = S&ソース=ウェブ&CD = 11&VED = 0ahUKEwimt5nt-P7QAhVr64MKHUdpDgEQFghWMAo&URL =のhttp%3A%2F%2Fwww.londonr.org%2Fdownload%2F%3Fid%3D97&USG = AFQjCNFemmTxQFHFidF4mzLWZWw43yuqmA&SIG2 = SC6hY1bLpOjmiEwvsxOfUw)。何を試しましたか? –

答えて

1

一つの方法は、xtabsを使用し、その後、 "整頓" フォームにデータセットを作ることです。まず、いくつかのクリーンアップ:

df[] <- lapply(df, as.character) # Convert factors to characters 
df[df == "NA" | df == "" | is.na(df)] <- NA # Make all blanks NAs 

を、きちんとしたデータセット:

library(tidyr) 
library(dplyr) 
out <- do.call(rbind, sapply(grep("^Col_Cold", names(df), value = T), function(x){ 
    vars <- c(x, grep("^Col_Hot", names(df), value = T)) 
    setNames(gather_(select(df, one_of(vars)), 
    key_col = x, 
    value_col = "value", 
    gather_cols = vars[-1])[, c(1, 3)], c("cold", "hot")) 
}, simplify = FALSE)) 

アイデアは、「ホット」列のそれぞれと「冷たい」列のそれぞれを作るために、「ペア」にあります長いデータセット。 outは次のようになります。

xtabs(~ cold + hot, na.omit(out)) 
#   hot 
# cold  Callus flutter infection medication twitching walking 
# Bump   0  1   0   0   1  0 
# hemaloma  1  0   1   0   0  0 
# muscle  0  1   0   1   2  0 
# pain   1  0   2   2   1  1 
# sleep   0  0   1   1   0  1 
+0

が働いていましたが、私の元のデータセットには、 'Col_Cold_x'というキーワードで' Cold'という列がありません。 'Cold'という単語のパターンが異なります。例えば、このCol_Coldのような列はほとんどなく、 Col_Cold_xy1 .... Col_Cold_xy10'、列名がCol_Cold1 ..... Col_Cold10であり、パターンがCol_Cold_Combine1 ... Col_Cold_Combine10などである列はほとんどありません。いくつかの問題。どのように私はコードを変更することができます上の任意のアドバイスですか?ありがとうウォン。 –

+0

この場合、 'grep'を使って関連する列を見つけることができます。編集を参照してください。 –

+0

トリックをしたWongに感謝します。 –

関連する問題