Writing data with multiple header rows #590
JanMarvin
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
This SO asks how to write data with multiple headers. There are two solutions to this issue, but I prefer writing such table in two steps. The second solution uses an
openxlsx2
option to convert strings to numerics on the fly. Note that although this option is supported, it can have unintended side effects.writing the table as two data frames
In R data frames, each column consists of a single data type. So if we have several rows of characters and append them over some numbers, the numbers will be converted to characters. Once we write this data to our output, we will write
"1"
instead of1
. If we write the header and the body in two steps, we can avoid the implicit conversion to characters.The problem is that we need to make sure that our header and body columns match and if we do not check that, we might generate nonsense. However, I strongly believe that one should check the data as soon as it is written.
everything in a single data frame
Still, there may be cases where we have everything in a single data frame. Multiple headers and numeric data mixed together. In this case
openxlsx2.string_nums
can be of help. If this option is set,openxlsx2
will check each cell to see if it can be expressed as numeric. If this is the case, it will convert on the fly.Beta Was this translation helpful? Give feedback.
All reactions