2016-04-12 52 views
1

我有以下表代碼更改比例表數值爲百分比

weather<-cbind(c(rep("rain",50),rep("sun",70),rep("rain",100),rep("sun",80))) 
prop.table(table(weather)) 

...這會導致下面的輸出:

rain sun 
0.5 0.5 

我如何更改代碼,以便輸出,而不是變成這樣:

rain sun 
50% 50% 

有什麼建議嗎?

回答

2

您可以使用sprintf。在你的榜樣,你可以這樣做:

sprintf("%2d%%", prop.table(table(weather))*100) 

更多數量的格式選項,檢查出sprintf文檔here

2
paste0(round(prop.table(table(weather))*100, 1),'%') 

round在這裏沒有必要,但它會是如果你的值有很多小數位。