R 将比例表值更改为百分比

R 将比例表值更改为百分比,r,R,我有下表代码 weather<-cbind(c(rep("rain",50),rep("sun",70),rep("rain",100),rep("sun",80))) prop.table(table(weather)) 如何更改代码,使输出变为: rain sun 50% 50% 有什么建议吗 paste0(round(prop.table(table(weather))*100, 1),'%') round在这里是不必要的,但是如果你的值有很多小数位的话 round在这

我有下表代码

weather<-cbind(c(rep("rain",50),rep("sun",70),rep("rain",100),rep("sun",80)))
prop.table(table(weather))
如何更改代码,使输出变为:

rain  sun 
 50%  50%
有什么建议吗

paste0(round(prop.table(table(weather))*100, 1),'%')
round
在这里是不必要的,但是如果你的值有很多小数位的话


round
在这里是不必要的,但是如果你的值有很多小数位的话就应该如此。

你可以使用
sprintf
。在您的示例中,您可以这样做:

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

有关更多数字格式选项,请查看
sprintf
文档

您可以为此使用
sprintf
。在您的示例中,您可以这样做:

sprintf("%2d%%", prop.table(table(weather))*100)
有关更多数字格式选项,请查看
sprintf
文档