2016-07-12 29 views
1

Beim Vergleich der beiden (Beispiel-) Datensätze unten versuche ich die Korrelation der Übereinstimmungen pro Spalte zu finden (wobei die NAs ignoriert werden). Ich weiß, dass ich das in einer verschachtelten for-Schleife tun kann, aber das würde den Prozess zu langsam machen, und ich bin unsicher, wie man dies in der data.table vektorisiert macht.Vergleich zweier Datensätze zur Ermittlung der Korrelation

adf <- structure(c(NA, "CT", NA, "CT", NA, NA, NA, NA, NA, "GG", NA, "CT", NA, "TC", NA, NA, NA, NA, NA, "AG", NA, "TT", NA, "TC", NA, NA, NA, NA, NA, "GG", NA, "CT", NA, "TC", NA, NA, NA, NA, NA, "GG", NA, "CT", NA, "CC", NA, NA, NA, NA, NA, "AG", NA, "CT", NA, "CC", NA, NA, NA, NA, NA, "AG", NA, "TT", NA, "CC", NA, NA, NA, NA, NA, "AG", NA, "CT", NA, "TC", NA, "GG", NA, "CC", NA, "GG", NA, "TT", NA, "TC", NA, "GG", NA, "CC", NA, "AG", NA, "TT", NA, "TT", NA, "GG", NA, NA, NA, "GG"), .Dim = c(10L, 10L), .Dimnames = list( c("7:100014471", "7:100038150", "7:100056646", "7:100079759",  "7:100103260", "7:100129337", "7:100145085", "7:100195980",  "7:100227528", "7:100256942"), c("100099681", "101666591",  "102247652", "102284616", "103582612", "104344528", "105729734",  "109897137", "112768301", "114724443"))) 
tdf <- structure(c(NA, "CC", NA, "CT", NA, NA, NA, NA, NA, "GG", NA, "CT", NA, "TC", NA, NA, NA, NA, NA, "AG", NA, "TT", NA, "TC", NA, NA, NA, NA, NA, "GG", NA, "CT", NA, "TC", NA, NA, NA, NA, NA, "GG", NA, "CT", NA, "CC", NA, NA, NA, NA, NA, "AG", NA, "CT", NA, "CC", NA, NA, NA, NA, NA, "AG", NA, "TT", NA, "CC", NA, NA, NA, NA, NA, "AG", NA, "CT", NA, "TC", NA, "GG", NA, "CC", NA, "GG", NA, "TT", NA, "TC", NA, "GG", NA, "CC", NA, "AG", NA, "TT", NA, "TT", NA, "GG", NA, NA, NA, "GG"), .Dim = c(10L, 10L), .Dimnames = list( c("7:100014471", "7:100038150", "7:100056646", "7:100079759",  "7:100103260", "7:100129337", "7:100145085", "7:100195980",  "7:100227528", "7:100256942"), c("100099681", "101666591",  "102247652", "102284616", "103582612", "104344528", "105729734",  "109897137", "112768301", "114724443"))) 

tdf[1:10, 1:10] 
#   100099681 101666591 102247652 102284616 103582612 104344528 105729734 109897137 112768301 114724443 
#7:100014471 NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  
#7:100038150 "CC"  "CT"  "TT"  "CT"  "CT"  "CT"  "TT"  "CT"  "TT"  "TT"  
#7:100056646 NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  
#7:100079759 "TC"  "TC"  "TC"  "TC"  "CC"  "CC"  "CC"  "TC"  "TC"  "TT"  
#7:100103260 NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  
#7:100129337 NA  NA  NA  NA  NA  NA  NA  "GG"  "GG"  "GG"  
#7:100145085 NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  
#7:100195980 NA  NA  NA  NA  NA  NA  NA  "CC"  "CC"  NA  
#7:100227528 NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  
#7:100256942 "GG"  "AG"  "GG"  "GG"  "AG"  "AG"  "AG"  "GG"  "AG"  "GG"  

adf[1:10, 1:10] 
#   100099681 101666591 102247652 102284616 103582612 104344528 105729734 109897137 112768301 114724443 
#7:100014471 NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  
#7:100038150 "CT"  "CT"  "TT"  "CT"  "CT"  "CT"  "TT"  "CT"  "TT"  "TT"  
#7:100056646 NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  
#7:100079759 "TC"  "TC"  "TC"  "TC"  "CC"  "CC"  "CC"  "TC"  "TC"  "TT"  
#7:100103260 NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  
#7:100129337 NA  NA  NA  NA  NA  NA  NA  "GG"  "GG"  "GG"  
#7:100145085 NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  
#7:100195980 NA  NA  NA  NA  NA  NA  NA  "CC"  "CC"  NA  
#7:100227528 NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  
#7:100256942 "GG"  "AG"  "GG"  "GG"  "AG"  "AG"  "AG"  "GG"  "AG"  "GG" 

Meine erwartete Ausgabe wäre in etwa so sein, nur die Korrelation Reihe wirklich benötigt wird, aber für Illustration/Klarheit habe ich hinzugefügt auch die Spiele/Mismatches.

  100099681 101666591 102247652 102284616 103582612 104344528 105729734 109897137 112768301 114724443 
matches 2   3   3   3   3   3   3   5   5   5 
mismatch 1   0   0   0   0   0   0   0   0   0 
cor  0.67  1   1   1   1   1   1   1   1   1 

Antwort

1

Wir können colsums verwenden:

colSums(adf == tdf, na.rm = TRUE)/
(
    colSums(adf == tdf, na.rm = TRUE) + 
    colSums(adf != tdf, na.rm = TRUE) 
) 

etwas sauberer Version:

# cleaner output 
matches <- colSums(adf == tdf, na.rm = TRUE) 
mismatches <- colSums(adf != tdf, na.rm = TRUE) 
allCnt <- matches + mismatches 

# output 
data.frame(
    matches, 
    mismatches, 
    allCnt, 
    myCor = matches/allCnt 
) 

#   matches mismatches allCnt  myCor 
# 100099681  2   1  3 0.6666667 
# 101666591  3   0  3 1.0000000 
# 102247652  3   0  3 1.0000000 
# 102284616  3   0  3 1.0000000 
# 103582612  3   0  3 1.0000000 
# 104344528  3   0  3 1.0000000 
# 105729734  3   0  3 1.0000000 
# 109897137  5   0  5 1.0000000 
# 112768301  5   0  5 1.0000000 
# 114724443  4   0  4 1.0000000