简体   繁体   中英

How to export data.frame column type to XLSX

I have a data set with 5 columns of type "CHR" and next 116 columns of type "NUM".

Basis the numeric columns I am creating further 64 calculated columns which are division or subtraction of one column with other basis my need. Issue I am facing is all these calculated columns gets stored as column type data.frame.

I tried checking the data and I can see it's a mix of numeric values as well as error values like NaN or INF etc. since at some places numbers are getting divided by 0.

On trying to export it using "Write_Xlsx" this exports the file in it's entirety however the values for the calculated columns come as blank in the excel file. I am not sure why and cant seem to make it work.

So far I have tried the below codes for converting the data.frames columns but all seem to throw error or convert all the numeric values to NA.

DFT$`VALMTHSPLY7-22` <- as.numeric((DFT$`VALMTHSPLY7-22`))
DFT$`VALMTHSPLY6-22` <- as.numeric(gsub(",", ".", DFT$`VALMTHSPLY6-22`))
as.data.frame(lapply(DFT, as.numeric))

Created on 2022-08-16 by the reprex package (v2.0.1)

I have tried few more, but seems like under the piles of my experiment, I can't seem to locate them.

I have 2 questions here:-

  1. How do export the whole dataset to excel without losing the numeric values in calculated columns?
  2. If nothing works, can we write something like 'iferror" kind of function in R to suppress the error values while making calculations because my limited understanding is probably R is treating a column having mixed values as data.frame.

Look forward to help here. Below is the code for my calculated columns (a part of it, since reproducing all 64 lines wont yield anything additional)

DFT$VALGR19 <- round((DFT[, 7]/DFT[, 6] - 1) * 100, 2)
DFT$MSVALGR22 <- round((DFT[, 68] - DFT[, 67]), 2)

Created on 2022-08-16 by the reprex package (v2.0.1)

EDIT

Here is the code used for writing to Excel,

write_xlsx(DFT, "C:\\Users\\I0510906\\Desktop\\Files\\RAuto\\Region-Data-MS-GR.xlsx")

Created on 2022-08-16 by the reprex package (v2.0.1)

Here is the code for calculation that I am using for creating columns:

DFT$VALGR19 <- round((DFT[, 7]/DFT[, 6] - 1) * 100, 2)

Created on 2022-08-16 by the reprex package (v2.0.1)

When I check the structure of the two calculated columns they come back as:- 'data.frame': 1126 obs. of 1 variable:

When I check the Table() property I can see it's a mix of numeric values, infinite and NaN values.

When I export them to excel, the column name gets exported to the excel but the cells are blank.

In order to get over this I am using the below code to convert the columns (since there are 64 in total and I dont know how to write loop for them):-

DFT$VALGR19 <- sapply(DFT$VALGR19, function(x) as.numeric(as.character(x)))

Created on 2022-08-16 by the reprex package (v2.0.1)

This converts the column into NUMERIC.

Using the same code for export, now I can export the file and all the values are exported. In excel I run a simple Search and replace for INF and NaN with blank.

However 2 things that I am still scratching my head over is:-

  1. Why is the calculated column going is data.frame
  2. How do I loop this method for 64 columns. Right now I have written 64 lines of code for individually converting each of the column which I know is not the best method.

Please let me know if something more be needed and would be happy to share.

This might be a workaround, as stated in your question the problem is that the null and NaN values in your r dataframe don't show up in your xlsx file. the following should work:

DFT <- format(DFT)
write_xlsx(DFT, "C:\\Users\\I0510906\\Desktop\\Files\\RAuto\\Region-Data-MS-GR.xlsx")

format() is a function 'for pretty printing' turning all values in your dataframe to characters. Which offcourse means you will have to change them back to numeric in the xlsx file when you want to do calculations. But this should work with printing all values from your dataframe to an xlsx file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM