change value to percentage of row in R [duplicate]

吃可爱长大的小学妹 提交于 2019-11-28 13:40:43

Seems a fair case for

df/rowSums(df)
#           A         B         C
# A 0.9000000 0.1000000 0.0000000
# B 0.3333333 0.3333333 0.3333333
# C 0.3333333 0.3333333 0.3333333

If you don't want so many digits after the dot set options(digits = 2) or use print(df/rowSums(df), digits = 2) or use round

round(df/rowSums(df), 2)
#      A    B    C
# A 0.90 0.10 0.00
# B 0.33 0.33 0.33
# C 0.33 0.33 0.33

Or as suggested by @akrun

round(prop.table(as.matrix(df1),1),2)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!