将字符插入文件的特定位置

时间:2017-05-20 08:45:49

标签: r optimization dataframe character

我有一个我为模型输入制作的文件。但我有一些缺少的角色。我想在模型可以读取它的位置插入这些字符 我检查的类是

file. class("outfile.txt")
[1] "character

所以不同代码后我的代码中的文件是

HYDRUS_Version=5
WaterFlow=0
SoluteTransport=0
Unsatchem=0
Unsatchem=0
HP1=0
HeatTransport=0
EquilibriumAdsorption=0
MobileImmobile=0
RootWaterUptake=0
RootGrowth=0
MaterialNumbers=0
SubregionNumbers=0
SpaceUnit=cm
TimeUnit=days
PrintTimes=160
NumberOfSolutes=0
InitialCondition=1
NumberOfNodes=101
ProfileDepth=120
ObservationNodes=160
GridVisible=160
SnapToGrid=160
ProfileWidth=160
LeftMargin=160
GridOrgX=160
GridOrgY=160
GridDX=160
GridDY=160

我需要的文件是[我想在其中插入4个字符,两个;[Main]位于开头,两个;[Profile]位于文件。我想要下面给出的最终文件。

;                           
[Main]                      `
HYDRUS_Version=5
WaterFlow=0
SoluteTransport=0
Unsatchem=0
Unsatchem=0
HP1=0
HeatTransport=0
EquilibriumAdsorption=0
MobileImmobile=0
RootWaterUptake=0
RootGrowth=0
MaterialNumbers=0
SubregionNumbers=0
SpaceUnit=cm
TimeUnit=days
PrintTimes=160
NumberOfSolutes=0
InitialCondition=1
;                                
[Profile]                        
NumberOfNodes=101
ProfileDepth=120
ObservationNodes=160
GridVisible=160
SnapToGrid=160
ProfileWidth=160
LeftMargin=160
GridOrgX=160
GridOrgY=160
GridDX=160
GridDY=160

1 个答案:

答案 0 :(得分:0)

你可以试试:      imported_text< - readLines(“outfile.txt”)

     [1] "HYDRUS_Version=5"        "WaterFlow=0"             "SoluteTransport=0"       "Unsatchem=0"             "Unsatchem=0"             "HP1=0"                  
  [7] "HeatTransport=0"         "EquilibriumAdsorption=0" "MobileImmobile=0"        "RootWaterUptake=0"       "RootGrowth=0"            "MaterialNumbers=0"      
 [13] "SubregionNumbers=0"      "SpaceUnit=cm"            "TimeUnit=days"           "PrintTimes=160"          "NumberOfSolutes=0"       "InitialCondition=1"     
[19] "NumberOfNodes=101"       "ProfileDepth=120"        "ObservationNodes=160"    "GridVisible=160"         "SnapToGrid=160"          "ProfileWidth=160"       
[25] "LeftMargin=160"          "GridOrgX=160"            "GridOrgY=160"            "GridDX=160"              "GridDY=160"             

并尝试c函数:

 imported_text2 <- c(";                            `here I want this`","[Main]                       `here I want this`",imported_text)

此外,你可以这样做:选择

final_text <- c(imported_text2[1:20],";                                `here I want this`","[Profile]                        `here I want this`",imported_text2[21:length(imported_text2)])

[1] ";                            `here I want this`"     "[Main]                       `here I want this`"     "HYDRUS_Version=5"                                   
 [4] "WaterFlow=0"                                         "SoluteTransport=0"                                   "Unsatchem=0"                                        
 [7] "Unsatchem=0"                                         "HP1=0"                                               "HeatTransport=0"                                    
[10] "EquilibriumAdsorption=0"                             "MobileImmobile=0"                                    "RootWaterUptake=0"                                  
[13] "RootGrowth=0"                                        "MaterialNumbers=0"                                   "SubregionNumbers=0"                                 
[16] "SpaceUnit=cm"                                        "TimeUnit=days"                                       "PrintTimes=160"                                     
[19] "NumberOfSolutes=0"                                   "InitialCondition=1"                                  ";                                `here I want this`"
[22] "[Profile]                        `here I want this`" "NumberOfNodes=101"                                   "ProfileDepth=120"                                   
[25] "ObservationNodes=160"                                "GridVisible=160"                                     "SnapToGrid=160"                                     
[28] "ProfileWidth=160"                                    "LeftMargin=160"                                      "GridOrgX=160"                                       
[31] "GridOrgY=160"                                        "GridDX=160"                                          "GridDY=160"  

修改

为了获得新的输出(根据OP请求):

c(";","[Main]",text[1:18],";","[Profile]",text[19:length(text)])