readxl icon indicating copy to clipboard operation
readxl copied to clipboard

option to repeat value in merged cells

Open jameshowison opened this issue 8 years ago • 4 comments

Moving discussion from: https://github.com/tidyverse/readxl/pull/220#issuecomment-298394447

Currently (I think) merged cells are handled by placing the value in the left-top cell of the range and placing NA in all other cells. Makes sense in many situations.

Another useful option might be to repeat the value across the previously merged range. That would, for example, be helpful in tidying files with multiple headers, as detailed here: https://howisonlab.github.io/datawrangling/Handling_multi_indexes.html#a-tidyverse-solution

However, with your suggestions from the other thread, tidyr::fill works for this sensibly if one does a little transposing:

# read, fill, and collapse 4 rows of multiple headers from merged cells
headers <- read_excel(filename, col_names = FALSE, na="..", n_max = 4)
# fill only works down or up, so have to transpose headers
long_headers = data.frame(t(headers))
long_headers <- fill(long_headers,1:4) # all four columns
headers <- data.frame(t(long_headers)) # back to vertical.

Nonetheless, an option to unmerge and fill all cells might still be useful.

jameshowison avatar May 01 '17 19:05 jameshowison

It would be quite handy to have some option like mergedcells = c("fill", "missing") with possibly some other sensible alternatives. I was going to suggest it as a feature if it weren't already suggested.

zyxdef avatar Jun 10 '17 22:06 zyxdef

+1. Merged cells are the bane of my existence.

slyrus avatar Oct 03 '19 02:10 slyrus

How about fill(..., .direction = "right")?

https://github.com/tidyverse/tidyr/blob/master/R/fill.R

jtr13 avatar Oct 12 '19 14:10 jtr13

+1. Can't believe this is not already implemented somehow.

EDIT: OK I just discovered the existence of tidyxl and unpivotr, so maybe this is the recommended way to go. Still, this adds some technical complexity to something that should be simple.

paulduf avatar Apr 16 '24 09:04 paulduf