2013-02-08 64 views
0

下面是一個簡單的代碼:R控制結構雙迴路

for (i in c("red", "green")) 
    for (j in c("blood", "trees")) { 
     k <- paste(i, j, sep=" ") 
     print(k) 
    } 
} 

我將它想只輸出"red blood""green trees"。剩下的組合將被丟棄。這可以在循環內完成嗎?

回答

5

你並不需要一個循環:

R> paste(c("red","green"), c("blood","trees")) 

[1] "red blood" "green trees"