'Can I make two headers? There is a problem with the type of data(numeric, character)

I need two header.

for example this is what I want excel file. but I want to keep all columns as numeric type.

a b c
aa bb cc
1 2 3
4 5 6
7 8 9

I try that first, I convert all columns to character. (by using as.character)

second, I put the second head (aa,bb,cc) in the first row.

third, I make a df to excel (by using read.csv)

In this case,

All columns are converted to character type. So There is a problem with the conversion to scientific notation.

Even when I open the file with notepad.

a b c
0.001 2 3
4 5 6
7 8 0.009

the character types, such a problem arises. but, the numeric type does not have such a problem.

r


Solution 1:[1]

I used " append "

(1) saving xlsx

wb <- openxlsx::createWorkbook()

  # create a sheet in the workbook
  openxlsx::addWorksheet(wb, 'sheet1')
  # add the data to the new sheet
  openxlsx::writeData(wb,'sheet1', col_df)
  openxlsx::writeData(wb,'sheet1', data_df,
                           startRow = 3,
                           colNames = F)

# saving the workbook
openxlsx::saveWorkbook(wb, './Data/final.xlsx', overwrite=T)

(2) saving csv

col_df %>% write.table('./Data/final.csv', na="",
                       sep=',',
                       row.names=F,
                       quote=F)

data_df %>%  write.table('./Data/final.csv',
                        sep=',',
                        quote=F,
                        row.names=F,
                        na="",
                        col.names=F,
                        append=T)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1