R语言 如何为特定年份的1和0生成计数?

e0bqpujr  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(119)

我想算出每年有多少个1和0,比如2017年,2018年和2019年。

ui7jx7zq

ui7jx7zq1#

quux <- data.frame(year = c(rep(2017, 5), rep(2018, 4)), paying = c(0,0,1,0,2,0,0,1,1))
quux
#   year paying
# 1 2017      0
# 2 2017      0
# 3 2017      1
# 4 2017      0
# 5 2017      2
# 6 2018      0
# 7 2018      0
# 8 2018      1
# 9 2018      1

table(quux$year, quux$paying > 0)
#       
#        FALSE TRUE
#   2017     3    2
#   2018     2    2

addmargins(table(quux$year, quux$paying > 0), 1)
#       
#        FALSE TRUE
#   2017     3    2
#   2018     2    2
#   Sum      5    4

相关问题