Mitra Rahmati
Mitra Rahmati

Reputation: 301

for double loop in R

for(i in 0:3) {
    for (j in quantile(0:80,probs=c(0,0.33,0.67,1))) {
        print(paste(i,j,sep=","))
    }
}

Generate:

"0,0"  
"0,26.4"  
"0,53.6"  
"0,80"  
"1,0"  
"1,26.4"  
.  
.  
.  

What about generating like below:

"0,0"  
"1,26.4"  
"2,53.6"  
"3,80"  
?  

Upvotes: 0

Views: 356

Answers (1)

Sacha Epskamp
Sacha Epskamp

Reputation: 47612

you don't really need a for loop for that:

paste(0:3,quantile(0:80,probs=c(0,0.33,0.67,1)),sep=",")

gives:

[1] "0,0"    "1,26.4" "2,53.6" "3,80"  

If you want to print it in that specific way you could just loop over this result and print.

Upvotes: 3

Related Questions