This problem has not solved yet, because the order does not match up. For each group unit number (10112 and 10114 in this case), I tried to copy to three more sub-units, adding one more number to the ID, 1,2, 3. (for example, 101121, 101122, 101123, 101141, 101142, 101143, and the same for the other group unit numbers) And then, I want to concatenate the three sub-units by order.
I made a smaller sample and my code below to explain the problem and my intended outcome. Thanks for any help.
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"
read_the_text <- scan(text = sample,
what = character(),
sep = "\n"); read_the_text
unit_ids <- as.vector(str_extract_all(read_the_text, "\\d{5}", simplify = TRUE))
unit_ids <- unit_ids[unit_ids != ""]
# add 1 to each unit id
new_ids1 <- paste0(unit_ids,1)
replacement1 <- setNames(new_ids1, unit_ids)
final_result1 <- str_replace_all(read_the_text, replacement1)
sink(file = "~/output1.txt")
invisible(x = mapply(FUN = cat, sep = "\n", final_result1))
sink()
# add 2 to each unit id
new_ids2 <- paste0(unit_ids,2)
replacement2 <- setNames(new_ids2, unit_ids)
final_result2 <- str_replace_all(read_the_text, replacement2)
sink(file = "~/output2.txt")
invisible(x = mapply(FUN = cat, sep = "\n", final_result2))
sink()
# add 3 to each unit id
new_ids3 <- paste0(unit_ids,3)
replacement3 <- setNames(new_ids3, unit_ids)
final_result3 <- str_replace_all(read_the_text, replacement3)
sink(file = "~/output3.txt")
invisible(x = mapply(FUN = cat, sep = "\n", final_result3))
sink()
Then I manually concatenated the three files one after another in a text editor, as I didn't know how to do it in R.
The outcome is like this:
outcome.concatenate <- "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
101122 2
1 0.1 0.6 0.3
7.07 2.01 0.26 0.13 4.68 0.56 0.96 1.28
101142 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
101123 2
1 0.1 0.6 0.3
7.07 2.01 0.26 0.13 4.68 0.56 0.96 1.28
101143 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
"
But what I wanted is like this:
intended.outcome <- "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
101122 2
1 0.1 0.6 0.3
7.07 2.01 0.26 0.13 4.68 0.56 0.96 1.28
101123 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
101142 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
101143 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
"