generating strings in a tricky order

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

generating strings in a tricky order

Taka Matzmoto
Hi R users

I like to generate some strings (a character vector) in a special way like

If i have 5 variables

"002.001",
"003.001", "003.002",
"004.001", "004.002", "004.003",
"005.001", "005.002", "005.003", "005.004"

so the created string vector's elements are

"002.001", "003.001", "003.002","004.001", "004.002", "004.003","005.001",
"005.002", "005.003", "005.004"

I tried to come up with for loop with two indexes (i and j) but I kept
failing to generate that kind of order of strings. The order of the element
in the character vector is very improtant.

Any advice or help would be appreciated

Thanks in advance

TM,

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: generating strings in a tricky order

jholtman
Is this what you want?

> n <- 5
> result <- character()
> for (i in 2:n){
+     for (j in 1:(i - 1)){
+         result <- c(result, sprintf("%03d:%03d", i, j))
+     }
+ }
> result
 [1] "002:001" "003:001" "003:002" "004:001" "004:002" "004:003" "005:001"
 [8] "005:002" "005:003" "005:004"



On 2/4/06, Taka Matzmoto <[hidden email]> wrote:

>
> Hi R users
>
> I like to generate some strings (a character vector) in a special way like
>
> If i have 5 variables
>
> "002.001",
> "003.001", "003.002",
> "004.001", "004.002", "004.003",
> "005.001", "005.002", "005.003", "005.004"
>
> so the created string vector's elements are
>
> "002.001", "003.001", "003.002","004.001", "004.002", "004.003","005.001",
> "005.002", "005.003", "005.004"
>
> I tried to come up with for loop with two indexes (i and j) but I kept
> failing to generate that kind of order of strings. The order of the
> element
> in the character vector is very improtant.
>
> Any advice or help would be appreciated
>
> Thanks in advance
>
> TM,
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



--
Jim Holtman
Cincinnati, OH
+1 513 247 0281

What the problem you are trying to solve?

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: generating strings in a tricky order

Gabor Grothendieck
In reply to this post by Taka Matzmoto
Try this:

x <- outer(1:5, 1:5, sprintf, fmt = "%03d.%03d")
sort(x[lower.tri(x)])

On 2/4/06, Taka Matzmoto <[hidden email]> wrote:

> Hi R users
>
> I like to generate some strings (a character vector) in a special way like
>
> If i have 5 variables
>
> "002.001",
> "003.001", "003.002",
> "004.001", "004.002", "004.003",
> "005.001", "005.002", "005.003", "005.004"
>
> so the created string vector's elements are
>
> "002.001", "003.001", "003.002","004.001", "004.002", "004.003","005.001",
> "005.002", "005.003", "005.004"
>
> I tried to come up with for loop with two indexes (i and j) but I kept
> failing to generate that kind of order of strings. The order of the element
> in the character vector is very improtant.
>
> Any advice or help would be appreciated
>
> Thanks in advance
>
> TM,
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html