Please use proper code formatting when posting, here is how to do it
This produces the desired output
library(stringr)
sample <- "10112 2
1 0.1 0.6 0.3
7.07 2.01 0.26 0.13 4.68 0.56 0.96 1.28
10114 5
1 0.2 0.3 0.5
9.78 8.64 8.33 7.6 10.57 7.16 9.05 8.58
2 0.3 0.4 0.3
4.95 5.91 4.01 3.82 5.94 4.41 3.53 5.8
1.4 0.67 5.22 0.96 1.23 2.52 1.36 4.81
10115 6
1 0.2 0.4 0.4
10.06 10.47 8.29 9.54 11.11 9.22 9 9.89
2 0.3 0.5 0.2
6.14 3.2 0.9 4.72 5.06 4.22 1.29 2.38
2 0.2 0.2 0.6
9.49 8.51 9.67 7.92 10.19 9.14 8.96 8.64"
read_the_text <- scan(text = sample,
what = character(),
sep = "\n"); read_the_text
unit_ids <- as.vector(str_extract_all(sample, "\\d{5}", simplify = TRUE))
new_ids <- paste0(unit_ids,1)
replacement <- setNames(new_ids, unit_ids)
final_result <- str_replace_all(sample, replacement)
sink(file = "~/output.txt")
invisible(x = mapply(FUN = cat, sep = "\n", final_result))
sink()
output.txt
content
101121 2
1 0.1 0.6 0.3
7.07 2.01 0.26 0.13 4.68 0.56 0.96 1.28
101141 5
1 0.2 0.3 0.5
9.78 8.64 8.33 7.6 10.57 7.16 9.05 8.58
2 0.3 0.4 0.3
4.95 5.91 4.01 3.82 5.94 4.41 3.53 5.8
1.4 0.67 5.22 0.96 1.23 2.52 1.36 4.81
101151 6
1 0.2 0.4 0.4
10.06 10.47 8.29 9.54 11.11 9.22 9 9.89
2 0.3 0.5 0.2
6.14 3.2 0.9 4.72 5.06 4.22 1.29 2.38
2 0.2 0.2 0.6
9.49 8.51 9.67 7.92 10.19 9.14 8.96 8.64