Home > Back-end >  How to create the output pattern of this example by concatenating string in R?
How to create the output pattern of this example by concatenating string in R?

Time:07-27

I have this code in R:

for (i in 0:4) {
    for (j in i:4) {
        b<-paste0("Y1 = ",i," && Y2 = ",j,",") 
        a<-paste0("X",i:j," ")  
        print(paste0(b,a))
    }
}
Output:
[1] "Y1 = 0 && Y2 = 0,X0 "
[1] "Y1 = 0 && Y2 = 1,X0 " "Y1 = 0 && Y2 = 1,X1 "
[1] "Y1 = 0 && Y2 = 2,X0 " "Y1 = 0 && Y2 = 2,X1 " "Y1 = 0 && Y2 = 2,X2 "
[1] "Y1 = 0 && Y2 = 3,X0 " "Y1 = 0 && Y2 = 3,X1 " "Y1 = 0 && Y2 = 3,X2 " "Y1 = 0 && Y2 = 3,X3 "
[1] "Y1 = 0 && Y2 = 4,X0 " "Y1 = 0 && Y2 = 4,X1 " "Y1 = 0 && Y2 = 4,X2 " "Y1 = 0 && Y2 = 4,X3 "
[5] "Y1 = 0 && Y2 = 4,X4 "
[1] "Y1 = 1 && Y2 = 1,X1 "
[1] "Y1 = 1 && Y2 = 2,X1 " "Y1 = 1 && Y2 = 2,X2 "
[1] "Y1 = 1 && Y2 = 3,X1 " "Y1 = 1 && Y2 = 3,X2 " "Y1 = 1 && Y2 = 3,X3 "
[1] "Y1 = 1 && Y2 = 4,X1 " "Y1 = 1 && Y2 = 4,X2 " "Y1 = 1 && Y2 = 4,X3 " "Y1 = 1 && Y2 = 4,X4 "
[1] "Y1 = 2 && Y2 = 2,X2 "
[1] "Y1 = 2 && Y2 = 3,X2 " "Y1 = 2 && Y2 = 3,X3 "
[1] "Y1 = 2 && Y2 = 4,X2 " "Y1 = 2 && Y2 = 4,X3 " "Y1 = 2 && Y2 = 4,X4 "
[1] "Y1 = 3 && Y2 = 3,X3 "
[1] "Y1 = 3 && Y2 = 4,X3 " "Y1 = 3 && Y2 = 4,X4 "
[1] "Y1 = 4 && Y2 = 4,X4 "

Expected output:

[1] "Y1 = 0 && Y2 = 0, X0,"
[1] "Y1 = 0 && Y2 = 1, X0 X1,"
[1] "Y1 = 0 && Y2 = 2, X0 X1 X2,"
[1] "Y1 = 0 && Y2 = 3, X0 X1 X2 X3,"
[1] "Y1 = 0 && Y2 = 4, X0 X1 X2 X3 X4,"
[1] "Y1 = 0 && Y2 = 2, X0 X2,"
[1] "Y1 = 0 && Y2 = 3, X0 X3,"
[1] "Y1 = 0 && Y2 = 4, X0 X4,"

[1] "Y1 = 1 && Y2 = 1, X1,"
[1] "Y1 = 1 && Y2 = 2, X1 X2,"
[1] "Y1 = 1 && Y2 = 3, X1 X2 X3,"
[1] "Y1 = 1 && Y2 = 4, X1 X2 X3 X4,"
[1] "Y1 = 1 && Y2 = 3, X1 X3,"
[1] "Y1 = 1 && Y2 = 4, X1 X4,"

[1] "Y1 = 2 && Y2 = 2, X2,"
[1] "Y1 = 2 && Y2 = 3, X2 X3,"
[1] "Y1 = 2 && Y2 = 4, X2 X3 X4,"
[1] "Y1 = 2 && Y2 = 4, X2 X4,"

[1] "Y1 = 3 && Y2 = 3, X3,"
[1] "Y1 = 3 && Y2 = 4, X3 X4,"

[1] "Y1 = 4 && Y2 = 4, X4,"

My goal is to generate an output exactly like the "Expected output:" block. The code you build presents a solution not very close to the expected result, but I believe it is on the right track. I just don't know how to solve this loop problem. Any ideas, considering that the code should be generic. in this example I used a loop of 0:4, but it could be 0:1000.

CodePudding user response:

There are a few issues with your code. One big thins is you need to specify a collapse argument to paste0() if you want it to return a single character string. Also it seems like you wanted an extra space after the comma in b so I added that in. The other main thing is you needed something to get the extra combinations of X values that don't include i:j. Here I used a conditional if statement to only induce that loop to iterate through the remaining values if i is less than top - 2 where top is the endpoint of the outer loop.

# empty list to catch results
results <- list()

# set upper limit of loop
top <- 4

# loop and assign output to results
for (i in 0:top) {
  for (j in i:top) {
    b <- paste0("Y1 = ",i," && Y2 = ",j,", ")
    a <- paste0("X",i:j, collapse = " ")
    
    results <- append(results, paste0(b,a,",", collapse = " "))
  }
  if (i < (top - 1)) {
    for (k in (i   2):top) {
      c <- paste0("X",i," ","X",k)
      results <- append(results, paste0(b,c, ",", collapse = " "))
    }
  }
}


cbind(results)
#>       results                            
#>  [1,] "Y1 = 0 && Y2 = 0, X0,"            
#>  [2,] "Y1 = 0 && Y2 = 1, X0 X1,"         
#>  [3,] "Y1 = 0 && Y2 = 2, X0 X1 X2,"      
#>  [4,] "Y1 = 0 && Y2 = 3, X0 X1 X2 X3,"   
#>  [5,] "Y1 = 0 && Y2 = 4, X0 X1 X2 X3 X4,"
#>  [6,] "Y1 = 0 && Y2 = 4, X0 X2,"         
#>  [7,] "Y1 = 0 && Y2 = 4, X0 X3,"         
#>  [8,] "Y1 = 0 && Y2 = 4, X0 X4,"         
#>  [9,] "Y1 = 1 && Y2 = 1, X1,"            
#> [10,] "Y1 = 1 && Y2 = 2, X1 X2,"         
#> [11,] "Y1 = 1 && Y2 = 3, X1 X2 X3,"      
#> [12,] "Y1 = 1 && Y2 = 4, X1 X2 X3 X4,"   
#> [13,] "Y1 = 1 && Y2 = 4, X1 X3,"         
#> [14,] "Y1 = 1 && Y2 = 4, X1 X4,"         
#> [15,] "Y1 = 2 && Y2 = 2, X2,"            
#> [16,] "Y1 = 2 && Y2 = 3, X2 X3,"         
#> [17,] "Y1 = 2 && Y2 = 4, X2 X3 X4,"      
#> [18,] "Y1 = 2 && Y2 = 4, X2 X4,"         
#> [19,] "Y1 = 3 && Y2 = 3, X3,"            
#> [20,] "Y1 = 3 && Y2 = 4, X3 X4,"         
#> [21,] "Y1 = 4 && Y2 = 4, X4,"

Created on 2022-07-26 by the reprex package (v2.0.1)

  • Related