openxlsx 公式翻译不正确

问题描述 投票:0回答:1

我想使用openxlsx实现以下公式: =IF(COUNTIF(E3:G3,"none")=3,"N/A",AVERAGE(IF(ISNUMBER(--E3:G3),-- E3:G3,""))) 到每一行 给定数据框,这个公式在 Excel 中完美运行,但是当我使用 openxlsx 时,它给出以下结果: =IF(COUNTIF(E3:G3, "") = 3, "不适用", 平均值(IF(ISNUMBER(--@E3:G3), --@E3:G3, ""))) 并且没有给我正确的答案,我该如何解决这个问题并摆脱@符号。

library(openxlsx)

# Create a new workbook and add a worksheet
wb <- createWorkbook()
addWorksheet(wb, "Sheet1")

# Example data: write some sample data to columns E, F, and G starting from row 1
example_data <- data.frame(
  E = c(1, NA, 3, "none", 5, 6, 7, 8, 9, 10), 
  F = c(11, 12, "none", 14, 15, 16, 17, 18, 19, 20),
  G = c(21, 22, 23, 24, "none", 26, 27, 28, 29, 30)
)
writeData(wb, "Sheet1", example_data, startCol = 5, startRow = 1)

# Determine the number of rows in your data
num_rows <- nrow(example_data)

# Function to create the formula with conditions
create_custom_formula <- function(row) {
  sprintf(
    "=IF(COUNTIF(E%d:G%d, \"*none*\") = 3, \"N/A\", AVERAGE(IF(ISNUMBER(--E%d:G%d), --E%d:G%d, \"\")))",
    row, row, row, row, row, row
  )
}

# Apply the function to each row and write the formulas to column H
lapply(1:num_rows, function(row) {
  writeFormula(wb, "Sheet1", x = create_custom_formula(row + 1), startCol = 8, startRow = row + 1)
})

# Set the column width for better visibility
setColWidths(wb, "Sheet1", cols = 8, widths = 15)

# Set the header for the new column (H1) to "Average"
writeData(wb, "Sheet1", "Average", startCol = 8, startRow = 1)

# Save the workbook
saveWorkbook(wb, "example_with_custom_averages.xlsx", overwrite = TRUE)
row formula openxlsx
1个回答
0
投票

您的公式是一个数组公式

--E3:G3
创建一个数组。在现代 Excel 中,这是隐藏的,公式两边没有大括号。在
openxlsx
中,您应该能够使用
array
参数。在使用
openxlsx2
的答案下方,您可以使用
cm
参数隐藏数组公式。

library(openxlsx2)

# separate numbers and strings in mixed data frame
options("openxlsx2.string_nums" = TRUE)

# -- data
example_data <- data.frame(
  E = c(1, NA, 3, "none", 5, 6, 7, 8, 9, 10), 
  F = c(11, 12, "none", 14, 15, 16, 17, 18, 19, 20),
  G = c(21, 22, 23, 24, "none", 26, 27, 28, 29, 30)
)

# --custom function
create_custom_formula <- function(row) {
  sprintf(
    "=IF(COUNTIF(E%d:G%d, \"none\") = 3, \"N/A\", AVERAGE(IF(ISNUMBER(--E%d:G%d), --E%d:G%d, \"\")))",
    row, row, row, row, row, row
  )
}

num_rows <- seq_len(nrow(example_data)) + 1

# -- create workbook with data
wb <- wb_workbook()$
  add_worksheet()$
  add_data(
    dims = wb_dims(x = example_data, from_col = 5),
    x = example_data, na.strings = NULL
  )$
  add_formula(
    x = create_custom_formula(num_rows), 
    dims = wb_dims(x = example_data, from_col = 8, from_row = 2),
    array = TRUE
  )

if (interactive()) wb$open()

enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.