在R中的write.xlsx时,将两个数据帧连接在一起并按颜色标记单元格

时间:2019-07-10 10:58:11

标签: r dplyr data.table tidyr openxlsx

我有两个数据框

prop=structure(list(KOD_NAR.id = structure(c(1L, 1L, 2L, 9L, 15L, 
16L, 17L, 3L, 4L, 18L, 5L, 6L, 19L, 20L, 7L, 8L, 21L, 10L, 11L, 
12L, 13L, 14L), .Label = c("", "-1", "04А  ", "04Б  ", "05А  ", 
"05Б  ", "07Д  ", "07С  ", "1", "10", "11", "12Г  ", "13", "15", 
"2", "3", "4", "5", "6", "7", "9"), class = "factor"), X1000494 = structure(c(4L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 3L), .Label = c("", "0.00425531914893617", "0.0106382978723404", 
"0.848936170212766"), class = "factor"), X1000495 = structure(c(4L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 3L), .Label = c("", "0.00651465798045603", "0.0293159609120521", 
"0.892508143322475"), class = "factor"), X1000496 = structure(c(4L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
1L, 1L, 1L, 1L, 3L), .Label = c("", "0.00366300366300366", "0.0366300366300366", 
"0.835164835164835"), class = "factor"), X1000500 = structure(c(3L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L), .Label = c("", "0.0161290322580645", "1.09032258064516"
), class = "factor")), .Names = c("KOD_NAR.id", "X1000494", "X1000495", 
"X1000496", "X1000500"), class = "data.frame", row.names = c(NA, 
-22L))

第二个

mash=structure(list(KOD_NAR.id = structure(c(1L, 8L, 16L, 17L, 18L, 
2L, 3L, 19L, 4L, 5L, 20L, 21L, 6L, 7L, 22L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L), .Label = c("-1", "04А  ", "04Б  ", "05А  ", "05Б  ", 
"07Д  ", "07С  ", "1", "10", "11", "12Г  ", "13", "15", "16", 
"16А  ", "2", "3", "4", "5", "6", "7", "9"), class = "factor"), 
    X1000494 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, 0L, 0L, NA), X1000495 = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 1L, 1L, NA), X1000496 = c(NA, NA, NA, NA, NA, 
    NA, NA, 0L, NA, NA, NA, NA, NA, NA, 0L, NA, NA, NA, NA, 2L, 
    0L, NA), X1000500 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0L, 0L, NA)), .Names = c("KOD_NAR.id", 
"X1000494", "X1000495", "X1000496", "X1000500"), class = "data.frame", row.names = c(NA, 
-22L))
我想以特殊方式合并这些数据帧,即: 文件结构是这样的:按行显示驱动程序的错误代码,按列显示驱动程序的ID。 如何通过相应的KOD_NAR.id将mash数据帧的标称数据加入prop数据帧的度量数据中。然后将带有颜色标记单元格的加载excel文件(write.xlsx)加入。如果在方括号中的单元格中为0,则颜色为绿色,如果为1,则颜色为黄色,如果为2,则单元格的颜色为红色

所以输出: enter image description here

有办法吗?

1 个答案:

答案 0 :(得分:1)

我敢肯定还有更多优雅的方法可以做到这一点(我不喜欢应用),但是这种方法行之有效。

您提供的数据集有一个小问题

library(openxlsx)   
library(tidyr)

style0=createStyle(fgFill = "green") #here you can change colors
style1=createStyle(fgFill = "yellow")
style2=createStyle(fgFill = "red")

prop$KOD_NAR.id=as.character(prop$KOD_NAR.id)

prop$KOD_NAR.id[1:2]=c("16","16a") #there must have been something wrong with the data, these two codes found in mash are not in prop
prop$KOD_NAR.id=factor(prop$KOD_NAR.id)
mash_long=mash %>% gather(key="ID",value="mash",-KOD_NAR.id)
prop_long=prop %>% gather(key="ID",value="prop",-KOD_NAR.id)

df=full_join(mash_long,prop_long) # KOD_NAR.id in prop had empty cells
df$mash[is.na(df$mash)]=""
df$prop[is.na(df$prop)]=""
df$cellcontent=ifelse(df$mash=="",df$prop,paste0(df$prop," (",df$mash,")"))

df_write=df %>% select(ID,cellcontent,KOD_NAR.id,starts_with("X")) %>% spread(ID,cellcontent)

wb=createWorkbook("workbook")
addWorksheet(wb,"Info")
writeData(wb,sheet = 1,df_write)
for(i in 2:ncol(df_write)){
  for(k in 1:nrow(df_write)){
    if(grepl("\\(",df_write[k,i])){
      addStyle(wb=wb,sheet = 1,rows = k+1,cols = i, #there will be a header row
               style=get(paste0("style",gsub(".*\\((\\d+)\\)","\\1",df_write[k,i]))))
    }
  }
}
saveWorkbook(wb,"example.xlsx",overwrite = T)