2016-08-01 21 views
2

Ich schreibe oft Papiere mit Korrelationsmatrizen. Ich möchte in der Lage sein, die Korrelationsmatrix nach Excel im XLS- oder XLSX-Format zu exportieren. Ich möchte auch fett formatierte Korrelationen formatieren, die einen Schwellenwert (z. B.> .2) erfüllen. Ich dachte, vielleicht bietet XLConnect die Funktionalität.Wie speichere ich einen R-Datenrahmen in Excel-Datei mit bestimmten Zellen in Fettdruck?

Um das Beispiel einfach zu machen, übernehmen die Datenrahmen wie folgt ist, und gehe ich davon zu fett wollen alle Zellen größer ist als 5.

x <- data.frame(matrix(1:9, nrow = 3)) 
# > x 
# X1 X2 X3 
# 1 1 4 7 
# 2 2 5 8 
# 3 3 6 9 

Als Nebenwirkung, stelle ich fest, dass Lösungen vorgeschlagen worden für cellbolding für Abschlags:

Ich habe auch festgestellt, diese Antwort, aber es ist nicht eine sehr allgemeine Lösung, dass es dauert ziemlich viel es auf die allgemeine Aufgabe anzupassen einen Datenrahmens und eine Formatierungsregel unter:

export data frames to Excel via xlsx with conditional formatting

+2

Bitte zeigen Sie alle Versuche an, die Sie unternommen haben. Ich arbeite kaum mit Tabellen, und das habe ich vor Jahren gelöst. Das [xlsx-Paket] (https://cloud.r-project.org/web/packages/xlsx/index.html) hat (hatte?) Beispiele, die genau das tun. –

+0

@DirkEddelbuettel \t Ich habe noch nicht wirklich angefangen. Aber wenn ich eine Lösung finde, werde ich eine Antwort posten, wie ich es oft tue. Meine anfängliche Annahme war, dass es keine einfache Antwort gibt, wenn Sie die Frage googlen, und es ist ein häufiger Anwendungsfall, also wäre es gut, wenn solch eine Ressource existiert. –

+1

Die Ressource existiert und ist im Paket [xlsx] (https://cloud.r-project.org/web/packages/xlsx/index.html) dokumentiert. Ich schlage vor, Sie schließen diese Frage. Es fügt nur Rauschen und Nullsignal hinzu. –

Antwort

2

xlsx_boldcells(x, x > 5) 
:

I die folgende Funktion geschaffen, die somit von @jota's answer here

xlsx_boldcells <- function(x, matches, file = "test.xlsx", sheetname = "sheet1") { 
    # x data.frame or matrix 
    # matches: logical data.frame or matrix of the same size indicating which cells to bold 
    # copy data frame to work book and load workbook 
    require(xlsx) 
    write.xlsx(x, file, sheetName=sheetname) 
    wb <- loadWorkbook(file)    

    # specify conditional formatting 
    # Note: this could be modified to apply different formatting 
    # see ?CellStyle 
    fo <- Font(wb, isBold = TRUE) 
    cs <- CellStyle(wb, font=fo) 

    # Get cell references 
    sheets <- getSheets(wb)    # get all sheets 
    sheet <- sheets[[sheetname]]   # get specific sheet 
    rows <- getRows(sheet, rowIndex=2:(nrow(x)+1)) # get rows 
    cells <- getCells(rows, colIndex = 2:(ncol(x)+1)) 

    # Matches to indexes 
    indm <- data.frame(which(matches, arr.ind = TRUE, useNames = FALSE)) 
    names(indm) <- c("row", "col") 
    # +1 required because row and column names occupy first rows and columns 
    indm$index <- paste(indm$row + 1, indm$col + 1, sep = ".") 

    # apply cell style 
    lapply(indm$index, function(ii) setCellStyle(cells[[ii]],cs)) 

    # save workbook 
    saveWorkbook(wb, file) 
} 

angepasst wurde, kann sie auf vorgeschlagene Problem angewendet werden,

Nachgeben:

excel sheet with bold

Oder es könnte auf die gemeinsame Korrelationsproblem (dh bolding große Korrelationen, zB größer als 0,6) angewendet werden, wie folgt:

data(mtcars) 
cors <- round(cor(mtcars), 2) 
xlsx_boldcells(cors, abs(cors) > .6 & cors!=1, file = "cormatrix.xlsx") 

bold cell formatted correlations using xlsx