Title: KableExtra conditionally formatting specific rows on a column
Tags: r;kable;kableextra
Question: I have just learnt KableExtra and know to how to use conditionally formating the entire column using mutate() as explained in the doc using mutate such as:

```mutate(
   mpg = cell_spec(mpg, background = ifelse(mpg &gt; 20, "red", "blue"))
)
```

But what I don't know is, how to change background colours of only certain rows in each column whilst all rows are being displayed.

For example, my data:

```df <- data.frame( region1 = c("A", sample(1:5,3)),
                  region2 = c("B", sample(1:5,3)),
                  region3 = c("C", sample(1:5,3)),
                  region4 = c("A", sample(1:5,3)) )
```

Now I want to format only second and third row. I dont want to change the color background of first and last row. These second and third row should be 'red' when above 1, or yellow when equal to 1 or green when below 1.

Some one could help me this?

Comment: I needed to display a single table where few rows are strings and rest are numbers. I had to format numers in the different columns. I dont know how to handle dataframe with mixed type. Should I keep them serperately? If so how will I display them as a single table..:-(

Comment: Will only the first row be letters and all other rows be numeric?

Comment: I try to adhere to the tidy data format (see this [PDF](http://vita.had.co.nz/papers/tidy-data.html) for Hadley Wickham's paper on the subject) – in short, rows are observations, while columns are variables – so I would keep letters and numbers together if they refer to the same variable.

Here is the accepted answer: Here's an example that ignores the first and last rows and colours according to value, like you say, but ignores letters.

First, I load the libraries.

```# Load libraries
library(knitr)
library(kableExtra)
```

Next, I create a dummy data frame.

```# Create data frame   
df <- data.frame( region1 = c(sample(c(-5:5, letters[1:5]), 10, replace = TRUE)),
                  region2 = c(sample(c(-5:5, letters[1:5]), 10, replace = TRUE)),
                  region3 = c(sample(c(-5:5, letters[1:5]), 10, replace = TRUE)),
                  region4 = c(sample(c(-5:5, letters[1:5]), 10, replace = TRUE)), stringsAsFactors = FALSE )
```

Here, I define the function for formatting cells. I ignore the first and last rows and check if the character is a letter or number, then colour accordingly.

```foo <- function(x, n, nmax){
  cell_spec(x, background = ifelse(is.na(as.numeric(x)), "white",
                                   ifelse(n == nmax | n == 1, "white",
                                          ifelse(x &gt; 1, "red",
                                                 ifelse(x < 1, "green", "yellow")))))
}
```

Finally, I apply the function.

```df %&gt;% 
  mutate_all(funs(foo(., n = row_number(), nmax = n()))) %&gt;% 
  kable(escape = FALSE) %&gt;% 
  kable_styling()
```

Comment for this answer: It is just fabulous. I have learnt and extended as per your help. Thanks a lot for saving my time. I was about to give up. Somebody told coding is poetry. But I see that in your function usage! thanks again!

Here is another answer: That's not a good design for a dataframe:  columns need to be all one type, so your numbers will be coerced to character.

Nevertheless, you can do what you ask for as follows.

```fixcol <- function(col) {
  x <- as.numeric(col[2:3])
  x <- cell_spec(x, background = ifelse(x &gt; 1, "red", ifelse(x == 1, "yellow", "green")))
  col[2:3] <- x
  col
}

df <- as.data.frame(lapply(df, fixcol))
kable(df, escape = FALSE)
```

Comment for this answer: Many thanks. I am getting an error : Error in `[<-.factor`(`*tmp*`, 2:3, value = NULL) : replacement has length zero. am  I doing anything wrong?
Comment for this answer: I increased the rows...now it turns...NA
Comment for this answer: You could print `col` in the `fixcol` function.  If it has fewer than 3 entries, you'd get a message like that, because `x` would end up too short.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:06/03/18', 'FavoriteCount': 4.0, 'PostTypeId': 1, 'Tags': '<r><kable><kableextra>', 'Title': 'KableExtra conditionally formatting specific rows on a column', 'ViewCount': 6625, 'id': 50665721, 'user_AboutMe': None, 'user_AccountId': 4684792.0, 'user_DisplayName': 'doctshind s', 'user_Reputation': 361.0, 'user_Views': 55.0, 'user_id': 3792463.0}}