2016-12-18 13 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 कीवर्ड हॉट के साथ कॉलम के अंतर्गत हैं और यह एसोसिएशन मैट्रिक्स के कॉलम बनाएगा। पंक्ति 3 में एक बार पंक्ति 1 में और फिर से: क्योंकि दर्द और संक्रमण के बीच सहयोग मूल dataframe में दो बार होता है

  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:

    अंतिम वांछित आउटपुट इस तरह दिखाई देनी चाहिए।

  • [pain, medication] = 2 क्योंकि दर्द और दवा के बीच सहयोग पंक्ति 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

वहाँ निकटता मैट्रिक्स पहले से ही बनाने पर जानकारी का एक टन है: [एक] (http://stackoverflow.com/ए/14850 9 86/115280 9), [दो] (https://www.r-bloggers.com/graph-from-sparse-adjacency-matrix/), [तीन (पीडीएफ)] (https://www.google .com/यूआरएल? सा = टी और RCT = जम्मू q = & ESRC = रों और स्रोत = वेब और सीडी = 11 & वेद = 0ahUKEwimt5nt-P7QAhVr64MKHUdpDgEQFghWMAo & url = http% 3A% 2F% 2Fwww.londonr.org% 2Fdownload% 2F% 3Fid% 3D97 और यूएसजी = 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 इस तरह दिखता है:

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

अंत में, xtabs का उपयोग वांछित आउटपुट बनाने के लिए:

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' क्रम में कीवर्ड' कोल्ड 'वाले कॉलम नहीं हैं, उदाहरण के लिए' कोल्ड 'शब्द के विभिन्न पैटर्न हैं उदाहरण के लिए इस' Col_Cold' जैसे कुछ कॉलम और कॉलम नाम वाले कुछ कॉलम हैं ' Col_Cold_xy1 .... Col_Cold_xy10' और कॉलम नामों के साथ कुछ कॉलम 'Col_Cold1 ..... Col_Cold10' और पैटर्न के साथ कुछ कॉलम' Col_Cold_Combine1 ...... Col_Cold_Combine10' आदि .... इसलिए इन विभिन्न कॉलम संयोजनों को शामिल करना कुछ समस्याएँ।मैं कोड कैसे बदल सकता हूं इस पर कोई सलाह? धन्यवाद वोंग। –

+0

इस मामले में हम प्रासंगिक कॉलम खोजने के लिए 'grep' का उपयोग कर सकते हैं। संपादन देखें। –

+0

धन्यवाद वोंग ने चाल की। –