如何在矩阵中插入%

时间:2020-04-18 19:34:53

标签: r data-manipulation

我有一个矩阵,我想将每个单元格乘以100,在其旁边插入“%”,并在末尾有一个矩阵。

    mtcars1 <- as.matrix(head(mtcars))
class(mtcars1)
mtcars1.per <- paste0(round(prop.table((mtcars1)), digits=2)*100,  "%")

采用矩阵形式的预期结果:

#                    mpg   cyl  disp      hp  drat  wt qsec vs  am   gear carb
#Mazda RX4         "1%" "0%" "7%"  "5%" "0%" "0%" "1%" "0%" "0%" "0%" "0%"

非常感谢。

1 个答案:

答案 0 :(得分:1)

如果最后需要一个matrix,请确保分配与[]一起使用以保留属性

mtcars1.per <- mtcars1
mtcars1.per[] <- paste0(round(prop.table((mtcars1)), digits=2)*100,  "%")