简体   繁体   English

如何将 data.frame 列类型导出到 XLSX

[英]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".我有一个数据集,其中包含 5 列“CHR”类型和接下来的 116 列“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.基于数字列,我正在创建另外 64 个计算列,这些列是我需要的其他基础的一列的除法或减法。 Issue I am facing is all these calculated columns gets stored as column type data.frame.我面临的问题是所有这些计算的列都存储为列类型 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.我尝试检查数据,我可以看到它是数字值以及错误值(如 NaN 或 INF 等)的混合,因为在某些地方,数字会被 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.在尝试使用“Write_Xlsx”导出它时,它会完整地导出文件,但是计算列的值在 excel 文件中显示为空白。 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.到目前为止,我已经尝试了以下代码来转换 data.frames 列,但似乎都抛出错误或将所有数值转换为 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)代表 package (v2.0.1) 于 2022 年 8 月 16 日创建

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?如何将整个数据集导出到 excel 而不会丢失计算列中的数值?
  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.如果没有任何效果,我们是否可以在 R 中编写类似“iferror”类型的 function 以在进行计算时抑制错误值,因为我有限的理解可能是 ZE1E1D3D40573127E9EE0480CAF1283D6 将混合值视为数据框。

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)下面是我的计算列的代码(其中一部分,因为复制所有 64 行不会产生任何额外的东西)

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)代表 package (v2.0.1) 于 2022 年 8 月 16 日创建

EDIT编辑

Here is the code used for writing to Excel,这是用于写入 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)代表 package (v2.0.1) 于 2022 年 8 月 16 日创建

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)代表 package (v2.0.1) 于 2022 年 8 月 16 日创建

When I check the structure of the two calculated columns they come back as:- 'data.frame': 1126 obs.当我检查两个计算列的结构时,它们返回为:-'data.frame':1126 obs。 of 1 variable: 1 个变量:

When I check the Table() property I can see it's a mix of numeric values, infinite and NaN values.当我检查 Table() 属性时,我可以看到它是数值、无限和 NaN 值的混合。

When I export them to excel, the column name gets exported to the excel but the cells are blank.当我将它们导出到 excel 时,列名被导出到 excel 但单元格为空白。

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):-为了克服这个问题,我使用下面的代码来转换列(因为总共有 64 个,我不知道如何为它们编写循环):-

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

Created on 2022-08-16 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 8 月 16 日创建

This converts the column into NUMERIC.这会将列转换为 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.在 excel 中,我运行一个简单的搜索并将 INF 和 NaN 替换为空白。

However 2 things that I am still scratching my head over is:-然而,我仍在摸不着头脑的两件事是:-

  1. Why is the calculated column going is data.frame为什么计算的列是data.frame
  2. How do I loop this method for 64 columns.如何将此方法循环用于 64 列。 Right now I have written 64 lines of code for individually converting each of the column which I know is not the best method.现在我已经编写了 64 行代码来单独转换我知道不是最好的方法的每一列。

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.这可能是一种解决方法,正如您的问题所述,问题是您的 r dataframe 中的 null 和 NaN 值不会显示在您的 xlsx 中。 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. format() 是一个 function '用于漂亮的打印' 将 dataframe 中的所有值转换为字符。 Which offcourse means you will have to change them back to numeric in the xlsx file when you want to do calculations.这意味着当您想要进行计算时,您必须将它们更改回 xlsx 文件中的数字。 But this should work with printing all values from your dataframe to an xlsx file.但这应该适用于将 dataframe 中的所有值打印到 xlsx 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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