gpt4 book ai didi

R > openxlsx::read.xlsx 以不同的方式处理值和公式

转载 作者:行者123 更新时间:2023-12-04 07:24:15 25 4
gpt4 key购买 nike

使用时openxlsx::read.xlsx在 R 中,我得到了不同的结果,读取 Excel 文件中的值与读取公式。
如果在单元格 A1:A3 中使用以下公式将 xlsx 工作簿另存为“test.xlsx”:


一种


第 1 列

甲乙

=A2


然后在R中:

library(openxlsx)
library(tibble)
my_file <- openxlsx::read.xlsx('test.xlsx')
tibble::view(my_file)
然后你应该看到:

Col1
A & B A &amp; B

有谁知道怎么改 &amp;回到 & (不修改原始xlsx)?
非常感谢

最佳答案

使用 dplyrstringr您可以使用 read.xlsx 修改从读取文件的 Excel 公式生成的“&”

library(openxlsx)

library(dplyr)
library(stringr)

read.xlsx("test.xlsx") %>%
mutate(Col1 = str_replace(Col1, "&amp;", "&"))
#> Col1
#> 1 A & B
#> 2 A & B

或者,您可以使用 readxl 来完全避免这个问题。读取文件。
library(readxl)

read_xlsx('test.xlsx')
#> # A tibble: 2 x 1
#> Col1
#> <chr>
#> 1 A & B
#> 2 A & B
创建于 2021-07-08 由 reprex package (v2.0.0)

关于R > openxlsx::read.xlsx 以不同的方式处理值和公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68306510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com