% -6ren">
gpt4 book ai didi

r - 字符串用 R 数据帧的列中的反斜杠和双引号替换字符

转载 作者:行者123 更新时间:2023-12-05 05:46:35 24 4
gpt4 key购买 nike

我有这个数据框:

df <- data.frame(
ID = c("1", "2", "3"),
option_json = c('{"thickness":"0.031 inches","tensile strength":"600 lb","size":"0.5 Inches x 7200 Feet"}', '{"thickness":"0.031 inches","tensile strength":"600 lb","size":"0.5 Inches x 7200 Feet"}' , '{"tensile strength":"600 lb","color":"Black","size":"0.5 Inches x 7200 Feet"}'))
  ID                                                                              option_json
1 1 {"thickness":"0.031 inches","tensile strength":"600 lb","size":"0.5 Inches x 7200 Feet"}
2 2 {"thickness":"0.031 inches","tensile strength":"600 lb","size":"0.5 Inches x 7200 Feet"}
3 3 {"tensile strength":"600 lb","color":"Black","size":"0.5 Inches x 7200 Feet"}

我想要这个数据框:

  ID                                                                option_json
1 1 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
2 2 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
3 3 {"tensile strength":"600 lb","color":"Black","size":"0.5\" x 7200'"}

我尝试使用 str_replace 和 gsub 来替换英寸,但双引号后面总是出现双反斜杠。不确定如何只用一个反斜杠替换字符串。

最佳答案

我认为在 R 中,如果其中一个反斜杠被转义,它们总是会一起打印两个反斜杠。当两个反斜杠一起显示时,这只是一种语法,表明它们应该被解释为字符“\”而不是转义字符。

为确认这一点,您可以尝试将数据框保存到文本文件中,您会看到字符串中实际上只有一个反斜杠。

df <- df %>% mutate(option_json = gsub(" inches", '\\\\"', option_json, ignore.case = T) %>% 
gsub(" Feet", "\\'", ., ignore.case = T))

write.table(df, "df.tsv", quote = F, row.names = F)

从“df.tsv”复制的输出

ID option_json
1 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
2 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
3 {"tensile strength":"600 lb","color":"Black","size":"0.5\" x 7200'"}

尝试打印“option_json”列

可以看到在每一个双引号"字符之前,都有一个转义字符\。而\\用来表示一个单个 \ 字符。

print(df$option_json)
[1] "{\"thickness\":\"0.031\\\"\",\"tensile strength\":\"600 lb\",\"size\":\"0.5\\\" x 7200'\"}"
[2] "{\"thickness\":\"0.031\\\"\",\"tensile strength\":\"600 lb\",\"size\":\"0.5\\\" x 7200'\"}"
[3] "{\"tensile strength\":\"600 lb\",\"color\":\"Black\",\"size\":\"0.5\\\" x 7200'\"}"

关于r - 字符串用 R 数据帧的列中的反斜杠和双引号替换字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71150310/

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