2014-07-16 57 views
1

簡單地說,我想打印1到5的所有組合作爲座標(x,y),並在其旁邊打印平均值。操縱和格式化數據幀的輸出

現在我一個月左右到R,這是我所管理:

combination <- combn(seq(1:5), 2) 
combination <- data.frame(combination) 
combination <- rbind(combination, combn(seq(1:5), 2, mean)) 
mapply(paste, combination[1,], combination[2,], MoreArgs = list(sep = ","), USE.NAMES = FALSE) 

所以我有我需要的一切數據幀,但什麼我難倒是得到它印像:

(1,2)1.5 (1,3)2 等

有人點我在對這個正確的方向?

大加讚賞, 克里斯

回答

1

像這樣的事情?

paste("(", combination[1,], ",", combination[2,], ") ", combination[3,], sep="") 

這將返回:

"(1,2) 1.5" "(1,3) 2" "(1,4) 2.5" "(1,5) 3" "(2,3) 2.5" "(2,4) 3" ... 
0

sprintf也是一個不錯的選擇要考慮,因爲你也可以很容易地指定您希望返回精度:

sprintf("(%.f, %.f) %.01f", ## Define the template you want to use 
     combination[1, ], ## Define (in order) where you want to get the 
     combination[2, ], ## values to fill in the template 
     combination[3, ]) 
# [1] "(1, 2) 1.5" "(1, 3) 2.0" "(1, 4) 2.5" "(1, 5) 3.0" "(2, 3) 2.5" 
# [6] "(2, 4) 3.0" "(2, 5) 3.5" "(3, 4) 3.5" "(3, 5) 4.0" "(4, 5) 4.5"