【问题标题】:How to append one column data frames to empty columns in worksheet of xlsx file in R?如何将一列数据框附加到 R 中 xlsx 文件工作表中的空列?
【发布时间】:2020-08-04 19:27:12
【问题描述】:

我想每天自动运行一个脚本,在其中我将一列数据框附加到 xlsx 文件的某些工作表中的现有表中。我有一个 xlsx 文件,其中包含有关 COVID-19 在荷兰的传播和严重程度的流行病学数据。因为我每天抓取的网站只发布当前的状态报告,所以我自己制作了带有趋势的数据框。每天我都想将一列数据框附加到工作表中,其中包含有关 COVID-19 感染人数的每日累积更新。此数据框需要附加在表末尾的第一个空列中。我试过这段代码:

addDataFrame(Meldingen, sheet = 'Meldingen', col.names = F, row.names = F , startRow = 1, startColumn = 10)

但此代码会覆盖 xlsx 文件中的数据,而不是将其附加到工作表中的表中。有人可以帮助我吗?谢谢你。

【问题讨论】:

    标签: r excel xlsx


    【解决方案1】:

    公式“Meldingen”中的引号将无济于事。您可以尝试这样的事情(以 iris 数据集为例)。要了解该过程,请准备一个名为“COVID19”的 .xlsx 文件,其中包含一张“Meldingen”表,其中包含 10 行随机数据(从 A1 开始)和您想要的列数(无标题)。虹膜数据集最后一列的前 10 行将被添加到文件中(在第一个空列中)。

    # Open the source .xlsx file (the receiver of the data)
    source=read.xlsx("COVID19.xlsx","Meldingen",header = FALSE)
    
    # Get the last computed column of the df which has to be added to the .xlsx file
    data=as.data.frame(iris[1:10,ncol(iris)])
    
    # Create the workbook and the sheet (even if they already exist)
    wb = createWorkbook()
    sheet = createSheet(wb, "Meldingen")
    
    # Get information about the existing sheet (the first empty column)
    i=dim(source)[2]+1
    
    # Add the data from the source .xlsx file
    addDataFrame(source, sheet=sheet, row.names=FALSE, col.names =FALSE,  startRow = 1)
    # Add the data from the R df
    addDataFrame(data, sheet=sheet, row.names=FALSE, col.names =FALSE,  startRow = 1, startColumn = i)
    
    # Overwrite the existing file
    saveWorkbook(wb, "COVID19.xlsx")
    

    data=as.data.frame(iris[1:10,ncol(iris)]) 替换为 data=as.data.frame(Meldingen[,ncol(iris)]) 假设“Meldingen”是 R 中数据框的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      相关资源
      最近更新 更多