【发布时间】:2021-12-30 03:52:23
【问题描述】:
假设我有一个df。
df = data.frame(status = c(1, 0, 0, 0, 1, 0, 0, 0),
stratum = c(1,1,1,1, 2,2,2,2),
death = 1:8)
> df
status stratum death
1 1 1 1
2 0 1 2
3 0 1 3
4 0 1 4
5 1 2 5
6 0 2 6
7 0 2 7
8 0 2 8
我想改变一个名为weights 的新变量。并应满足以下条件:
-
weights应该在stratum组中发生变异。 - 当
status为1时,weights值应返回death值。
我期望的应该是这样的:
df_wanted = data.frame(status = c(1, 0, 0, 0, 1, 0, 0, 0),
stratum = c(1,1,1,1, 2,2,2,2),
death = 1:8,
weights = c(1,1,1,1, 5,5,5,5))
> df_wanted
status stratum death weights
1 1 1 1 1
2 0 1 2 1
3 0 1 3 1
4 0 1 4 1
5 1 2 5 5
6 0 2 6 5
7 0 2 7 5
8 0 2 8 5
我不知道怎么写代码。
任何帮助将不胜感激!
【问题讨论】: