rnorouzian
rnorouzian

Reputation: 7517

Creating ordered, numbered strings in R?

I was wondering how I could create the entire following ordered, numbered string "list" in R?

I could think of noquote(paste0("p.b", 1:7, rep(".", 7), rep(1, 7))) but this just creates the first line of the string not the entire 4 lines of the following string:

p.b1.1 ; p.b2.1 ; p.b3.1 ; p.b4.1 ; p.b5.1 ; p.b6.1 ; p.b7.1 
p.b1.2 ; p.b2.2 ; p.b3.2 ; p.b4.2 ; p.b5.2 ; p.b6.2 ; p.b7.2  
p.b1.3 ; p.b2.3 ; p.b3.3 ; p.b4.3 ; p.b5.3 ; p.b6.3 ; p.b7.3  
p.b1.4 ; p.b2.4 ; p.b3.4 ; p.b4.4 ; p.b5.4 ; p.b6.4 ; p.b7.4

Note: I just used ; to distinguish between each object, I won't need the the ; in my final, generated strings. Again, note these need to be a list.

Upvotes: 0

Views: 40

Answers (2)

Nathan Werth
Nathan Werth

Reputation: 5263

outer(1:4, 1:7, sprintf, fmt = 'p.b%d.%d')

Upvotes: 1

d.b
d.b

Reputation: 32548

noquote(sapply(1:4, function(i)
    paste((paste("p.b", 1:7, ".", rep(i, 7), sep = "")), collapse = " ; ")))
#[1] p.b1.1 ; p.b2.1 ; p.b3.1 ; p.b4.1 ; p.b5.1 ; p.b6.1 ; p.b7.1
#[2] p.b1.2 ; p.b2.2 ; p.b3.2 ; p.b4.2 ; p.b5.2 ; p.b6.2 ; p.b7.2
#[3] p.b1.3 ; p.b2.3 ; p.b3.3 ; p.b4.3 ; p.b5.3 ; p.b6.3 ; p.b7.3
#[4] p.b1.4 ; p.b2.4 ; p.b3.4 ; p.b4.4 ; p.b5.4 ; p.b6.4 ; p.b7.4

OR

noquote(t(sapply(1:4, function(i)
    (paste("p.b", 1:7, ".", rep(i, 7), sep = "")))))
#     [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]  
#[1,] p.b1.1 p.b2.1 p.b3.1 p.b4.1 p.b5.1 p.b6.1 p.b7.1
#[2,] p.b1.2 p.b2.2 p.b3.2 p.b4.2 p.b5.2 p.b6.2 p.b7.2
#[3,] p.b1.3 p.b2.3 p.b3.3 p.b4.3 p.b5.3 p.b6.3 p.b7.3
#[4,] p.b1.4 p.b2.4 p.b3.4 p.b4.4 p.b5.4 p.b6.4 p.b7.4

Upvotes: 1

Related Questions