row color of data.frame

大憨熊 提交于 2019-12-11 02:02:39

问题


I'm trying to highlight the rows of the data.frame (testdf) according to the value in the column with the text values ("Type")

testdf<-data.frame(Type=c("NON_BRAND", "BRAND", "BRAND", "NON_BRAND"),CA=c(500,890,780,240), Campaign=c("A", "B", "A", "B"))

I can't achieve this using heatmap, as I need to color the whole row according to the word in the column "Type". (NON_BRAND rows color in blue and BRAND rows in yellow, for example).

My output is a pdf or html file using R Markdown.

Does anyone know if there is such possibility to color the data.frame?

Any help would be greatly appreciated.


回答1:


lazyWeave is a little clunky in it's current CRAN version, but I'm working on it and hope to have an update within a month or so that will take care of some of it's oddities.

I feel like xtable can do this too, though I can't remember the specifics. If xtable will do it, I'd recommend using that most of the time. It's a bit cleaner than lazyWeave.

---
title: "Untitled"
output: html_document
---

```{r}
library(lazyWeave)
options(lazyReportFormat="html")
testdf<-data.frame(Type=c("NON_BRAND", "BRAND", "BRAND", "NON_BRAND"),
                   CA=c(500,890,780,240), 
                   Campaign=c("A", "B", "A", "B"))
```

```{r, results='asis'}
lazy.matrix(testdf,
            rcol= 1:nrow(testdf),
            usecol = ifelse(testdf$Type == "NON_BRAND", "blue", "yellow"))
```


来源:https://stackoverflow.com/questions/31517012/row-color-of-data-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!