lundi 24 juin 2019

How to loop through block of lines in text using R

I have a large text file which has block of lines. Below I added a sample of text file. Every third line of blocks, there is a special given soil profile name like "HC_GEN0011" Then, every seventh line of blocks there is a value under "SLOC" string. I would like to change this value for first layer with new SLOC values which are from hcprofname data frame.

1 *ET03899254    ETH   SandClayL   200    ISRIC soilgrids + HC27                                                                                                                                          
2 @SITE        COUNTRY          LAT     LONG SCS Family                                                                                                                                                   
3  -99              ET       14.792   37.875     HC_GEN0011                                                                                                                                               
4 @ SCOM  SALB  SLU1  SLDR  SLRO  SLNF  SLPF  SMHB  SMPX  SMKE                                                                                                                                            
5     BK  0.10  6.00  0.50 75.00  1.00  1.00 SA001 SA001 SA001                                                                                                                                            
6 @  SLB  SLMH  SLLL  SDUL  SSAT  SRGF  SSKS  SBDM  SLOC  SLCL  SLSI  SLCF  SLNI  SLHW  SLHB  SCEC  SADC                                                                                                  
7      5 A     0.165 0.273 0.401  1.00  0.53  1.32  2.78 27.41 21.92 -99.0  0.12  6.61 -99.0  22.1 -99.0                                                                                                  
8     15 A     0.177 0.287 0.404  0.85  0.41  1.33  2.36 29.54 20.91 -99.0  0.09  6.68 -99.0  19.4 -99.0                                                                                                  
9     30 AB    0.192 0.303 0.410  0.70  0.31  1.36  1.80 32.03 19.95 -99.0  0.07  6.78 -99.0  18.8 -99.0                                                                                                  
10    60 BA    0.207 0.320 0.416  0.50  0.23  1.42  1.15 34.50 18.92 -99.0  0.06  6.89 -99.0  19.7 -99.0                                                                                                  
11   100 B     0.206 0.319 0.415  0.38  0.23  1.48  0.68 34.47 18.29 -99.0  0.05  7.02 -99.0  19.8 -99.0                                                                                                  
12   200 BC    0.197 0.306 0.410  0.05  0.28  1.53  0.39 32.81 17.92 -99.0  0.05  7.21 -99.0  19.8 -99.0                                                                                                  


hcprofname;

Soil profile   SLOC_new
HC_GEN0002     1.50
HC_GEN0003     1.75
HC_GEN0011     1.40

I couldn't get what I want with this code.

Workdir <- "...\\ET.SOL"
f <- readLines(Workdir)
for ( i in 1:length(seq(3, length(f), 13))){

        for (j in 1:length(seq(7, length(f), 13))){

          ifelse((grepl("11", (substring(f[i], 48, 58)))),

          (f <- gsub((substring(f[j], 51, 54)) , hcprofname[3,2], f)) , f )}
}

Desired result;

1 *ET03899254    ETH   SandClayL   200    ISRIC soilgrids + HC27                                                                                                                                          
2 @SITE        COUNTRY          LAT     LONG SCS Family                                                                                                                                                   
3  -99              ET       14.792   37.875     HC_GEN0011                                                                                                                                               
4 @ SCOM  SALB  SLU1  SLDR  SLRO  SLNF  SLPF  SMHB  SMPX  SMKE                                                                                                                                            
5     BK  0.10  6.00  0.50 75.00  1.00  1.00 SA001 SA001 SA001                                                                                                                                            
6 @  SLB  SLMH  SLLL  SDUL  SSAT  SRGF  SSKS  SBDM  SLOC  SLCL  SLSI  SLCF  SLNI  SLHW  SLHB  SCEC  SADC                                                                                                  
7      5 A     0.165 0.273 0.401  1.00  0.53  1.32  1.40 27.41 21.92 -99.0  0.12  6.61 -99.0  22.1 -99.0                                                                                                  
8     15 A     0.177 0.287 0.404  0.85  0.41  1.33  2.36 29.54 20.91 -99.0  0.09  6.68 -99.0  19.4 -99.0                                                                                                  
9     30 AB    0.192 0.303 0.410  0.70  0.31  1.36  1.80 32.03 19.95 -99.0  0.07  6.78 -99.0  18.8 -99.0                                                                                                  
10    60 BA    0.207 0.320 0.416  0.50  0.23  1.42  1.15 34.50 18.92 -99.0  0.06  6.89 -99.0  19.7 -99.0                                                                                                  
11   100 B     0.206 0.319 0.415  0.38  0.23  1.48  0.68 34.47 18.29 -99.0  0.05  7.02 -99.0  19.8 -99.0                                                                                                  
12   200 BC    0.197 0.306 0.410  0.05  0.28  1.53  0.39 32.81 17.92 -99.0  0.05  7.21 -99.0  19.8 -99.0                                                                                                  


Thank you for your help in advance!

Aucun commentaire:

Enregistrer un commentaire