如何在 R 中写入或更改文件中的某些字符?

发布于 2025-01-10 23:00:44 字数 1066 浏览 0 评论 0原文

我有一个 PRM 文件,它是一种文本文件。我想改变其中的一些字符。例如,“md = minf;”到“md = maxf;”和“ls = 1”到“ls = 3”。 你能指导我如何改变它吗?我不知道在这种情况下如何使用 WriteLines 函数?

> setwd("C:/Users/Documents/CONN")
> fileName <- "Hmin.PRM"
> conn <- file(fileName,open="r")
> linn <-readLines(conn)
> for (i in 1:length(linn)){
+   print(linn[i])
+ }
[1] ""
[1] "begin_pop = \"p1\";"
[1] "   beginfounder;"
[1] "      male   [n =   20, pop = \"hp\"];"
[1] "      female [n = 400, pop = \"hp\"];"
[1] "   endfounder;"
[1] "   ls  = 1;           "
[1] "   pmp = 0.5;                 "
[1] "   ng  = 10;                  "
[1] "   md  = minf;                 "
[1] "   sr  = 0.5;                 "
[1] "   dr  = 0.3;                 "
[1] "   sd  = ebv /h;             "
[1] "   cd  = ebv /l;              "
[1] "   ebvest = true_av;"
[1] "   begpop;"
[1] "        ld /maft 0.1;"
[1] "\t   crossover;"
[1] "        data;"
[1] "        stat;"
[1] "        genotype/gen 8 9 10;"
[1] "   endpop;"
[1] "end_pop;"
[1] "  "
> close(conn)

I have a PRM file that is a kind of text file. I want to change some characters in it. For example, " md = minf; " to "md = maxf;" and "ls = 1" to "ls = 3".
Can you guide me on how can I change it? I don't know how can I use WriteLines function in this situation?

> setwd("C:/Users/Documents/CONN")
> fileName <- "Hmin.PRM"
> conn <- file(fileName,open="r")
> linn <-readLines(conn)
> for (i in 1:length(linn)){
+   print(linn[i])
+ }
[1] ""
[1] "begin_pop = \"p1\";"
[1] "   beginfounder;"
[1] "      male   [n =   20, pop = \"hp\"];"
[1] "      female [n = 400, pop = \"hp\"];"
[1] "   endfounder;"
[1] "   ls  = 1;           "
[1] "   pmp = 0.5;                 "
[1] "   ng  = 10;                  "
[1] "   md  = minf;                 "
[1] "   sr  = 0.5;                 "
[1] "   dr  = 0.3;                 "
[1] "   sd  = ebv /h;             "
[1] "   cd  = ebv /l;              "
[1] "   ebvest = true_av;"
[1] "   begpop;"
[1] "        ld /maft 0.1;"
[1] "\t   crossover;"
[1] "        data;"
[1] "        stat;"
[1] "        genotype/gen 8 9 10;"
[1] "   endpop;"
[1] "end_pop;"
[1] "  "
> close(conn)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷血 2025-01-17 23:00:44
linn <- c("begin_pop = \\p1\\;", "   beginfounder;", "      male   [n =   20, pop = \\hp\\];", "      female [n = 400, pop = \\hp\\];", "   endfounder;", "   ls  = 1;", "   pmp = 0.5;", "   ng  = 10;", "   md  = minf;", "   sr  = 0.5;", "   dr  = 0.3;", "   sd  = ebv /h;", "   cd  = ebv /l;", "   ebvest = true_av;", "   begpop;", "        ld /maft 0.1;", "\t   crossover;", "        data;", "        stat;", "        genotype/gen 8 9 10;", "   endpop;", "end_pop;")

gsub("\\b(ls\\s*=\\s*)1", "\\1 3",
     gsub("\\b(md\\s*=\\s*)minf", "\\1maxf", linn))
#  [1] "begin_pop = \\p1\\;"                    "   beginfounder;"                      
#  [3] "      male   [n =   20, pop = \\hp\\];" "      female [n = 400, pop = \\hp\\];" 
#  [5] "   endfounder;"                         "   ls  =  3;"                          
#  [7] "   pmp = 0.5;"                          "   ng  = 10;"                          
#  [9] "   md  = maxf;"                         "   sr  = 0.5;"                         
# [11] "   dr  = 0.3;"                          "   sd  = ebv /h;"                      
# [13] "   cd  = ebv /l;"                       "   ebvest = true_av;"                  
# [15] "   begpop;"                             "        ld /maft 0.1;"                 
# [17] "\t   crossover;"                         "        data;"                         
# [19] "        stat;"                          "        genotype/gen 8 9 10;"          
# [21] "   endpop;"                             "end_pop;"                              

正则表达式的解释:

  • \\b是一个单词边界,所以我们匹配md 但不是 amd
  • \\s* 是零个或多个空格
  • (..) 是一个匹配组,(在本例中)我们希望将其带回替代品;
  • \\1 正在回忆第一个匹配组,
  • 这是我在 ls =3 之间引入空格的唯一原因(而我没有添加maxf 之前的空格)是我希望 \\1\\13 之间在视觉上绝对没有歧义,建议 13 个匹配组。 R 对后者没有问题,但我想我应该在正则表达式中保持清晰。
linn <- c("begin_pop = \\p1\\;", "   beginfounder;", "      male   [n =   20, pop = \\hp\\];", "      female [n = 400, pop = \\hp\\];", "   endfounder;", "   ls  = 1;", "   pmp = 0.5;", "   ng  = 10;", "   md  = minf;", "   sr  = 0.5;", "   dr  = 0.3;", "   sd  = ebv /h;", "   cd  = ebv /l;", "   ebvest = true_av;", "   begpop;", "        ld /maft 0.1;", "\t   crossover;", "        data;", "        stat;", "        genotype/gen 8 9 10;", "   endpop;", "end_pop;")

gsub("\\b(ls\\s*=\\s*)1", "\\1 3",
     gsub("\\b(md\\s*=\\s*)minf", "\\1maxf", linn))
#  [1] "begin_pop = \\p1\\;"                    "   beginfounder;"                      
#  [3] "      male   [n =   20, pop = \\hp\\];" "      female [n = 400, pop = \\hp\\];" 
#  [5] "   endfounder;"                         "   ls  =  3;"                          
#  [7] "   pmp = 0.5;"                          "   ng  = 10;"                          
#  [9] "   md  = maxf;"                         "   sr  = 0.5;"                         
# [11] "   dr  = 0.3;"                          "   sd  = ebv /h;"                      
# [13] "   cd  = ebv /l;"                       "   ebvest = true_av;"                  
# [15] "   begpop;"                             "        ld /maft 0.1;"                 
# [17] "\t   crossover;"                         "        data;"                         
# [19] "        stat;"                          "        genotype/gen 8 9 10;"          
# [21] "   endpop;"                             "end_pop;"                              

Explanation of the regex:

  • \\b is a word-boundary, so we match md but not amd;
  • \\s* is zero-or-more blank-space
  • (..) is a match-group, which (in this case) we want to bring back in the replacement;
  • \\1 is recalling the first match-group
  • the only reason I introduced a space between ls = and 3 (whereas I did not add a space before maxf) is that I wanted absolutely no ambiguity visually between \\1 and \\13 suggesting 13 match-groups. R didn't have a problem with the latter, but I thought I'd keep it clear in the regex.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文