从 R 中 data.table 的第一行减去
subtract from first row in a data.table in R
我有以下 table 并且我需要如图所示的输出。基本上,从第 2 行、第 3 行、第 4 行等中减去 "num_date"
列第一行中的值
Table1:
Year num_date
2016 16703
2016 16705
2016 16706
2016 16708
.
.
Output:
Year num_date
2016 0
2016 2
2016 3
2016 5
有人可以帮我在 R 中实现这个吗?
提前致谢,
您可以通过使用 indexing [1]
提取第一个值,从列中减去它,然后将其赋值回来:
df$num_date = df$num_date - df$num_date[1]
df$num_date = df$num_date - df$num_date[1]
df
# Year num_date
#1 2016 0
#2 2016 2
#3 2016 3
#4 2016 5
我有以下 table 并且我需要如图所示的输出。基本上,从第 2 行、第 3 行、第 4 行等中减去 "num_date"
列第一行中的值
Table1:
Year num_date
2016 16703
2016 16705
2016 16706
2016 16708
.
.
Output:
Year num_date
2016 0
2016 2
2016 3
2016 5
有人可以帮我在 R 中实现这个吗?
提前致谢,
您可以通过使用 indexing [1]
提取第一个值,从列中减去它,然后将其赋值回来:
df$num_date = df$num_date - df$num_date[1]
df$num_date = df$num_date - df$num_date[1]
df
# Year num_date
#1 2016 0
#2 2016 2
#3 2016 3
#4 2016 5