user3825422
user3825422

Reputation: 183

row color of data.frame

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.

Upvotes: 2

Views: 1207

Answers (1)

Benjamin
Benjamin

Reputation: 17369

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"))
```

Upvotes: 1

Related Questions