2013-04-09 64 views
0

有沒有辦法改變dcast函數名變量的默認方式?例如R中的自定義變量名reshape2

require(reshape2) 
x = data.frame(id=1:2, t=1:5, v=10:1) 
m = melt(x, id.vars = c("id", "t")) 
cx = dcast(m, t ~ variable + id) 
print(cx) 

# t v_1 v_2 
#1 1 10 5 
#2 2 4 9 
#3 3 8 3 
#4 4 2 7 
#5 5 6 1 

我想v_1命名v_id_1什麼的。

回答

2

我不認爲這是可能使用dcast但你可以使用gsub這樣的:

colnames(cx) <- gsub('(.*)_(*.)','\\1_id_\\2',colnames(cx)) 
> cx 
    t v_id_1 v_id_2 
1 1  10  5 
2 2  4  9 
3 3  8  3 
4 4  2  7 
5 5  6  1