如何将每个i重新编码为i-1

问题描述

我有类似的数据

table(data$num)
# 1  2  3  4  5  6  7 ....         100
# 10 2  13 2  7  8  19              2

我想将每个i重新编码为i-1,例如

table(data$num)
# 0  1  2  3  4  5  6 ....         99
# 10 2  13 2  7  8  19              2

我该怎么做?

解决方法

使用:

table(data$num - 1)

mtcars为例:

table(mtcars$cyl)

# 4  6  8 
#11  7 14 
table(mtcars$cyl - 1)

# 3  5  7 
#11  7 14