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

How can I write or change some characters in my file in R?

我有一个 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)
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;"                              

regex的解释:

  • \b 是一个 word-boundary,所以我们匹配 md 但不匹配 amd;
  • \s* 是 zero-or-more blank-space
  • (..) 是一个 match-group,(在本例中)我们要将其带回替换;
  • \1正在回忆第一个match-group
  • 我在 ls =3 之间引入 space 的唯一原因(而我没有在 maxf 之前添加 space)是我\1\13 之间绝对没有歧义,建议 13 match-groups。 R 对后者没有问题,但我想我会在正则表达式中保持清楚。