简体   繁体   English

如何在 r 中改变具有不同条件的新变量

[英]how to mutate new variables with different conditions in r

Say I have a df .说我有一个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

I want to mutate a new variable named weights .我想改变一个名为weights的新变量。 And it should meet the following conditions:并且应满足以下条件:

  1. weights should be mutated in stratum group. weights应该在stratum组中发生突变。
  2. the weights value should return death value when the status is 1 .status1时, weights值应返回death值。

What I expected should like this:我期望的应该是这样的:

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

I do not know how to write the code.我不知道如何编写代码。

Any help will be highly appreciated!任何帮助将不胜感激!

You may get the death value where status = 1 .您可能会在status = 1处获得death值。

library(dplyr)

df %>%
  group_by(stratum) %>%
  mutate(weights = death[status == 1]) %>%
  ungroup

The above works because there is exactly 1 value in each group where status = 1 .上述方法有效,因为在status = 1的每个组中恰好有 1 个值。 If there are 0 or more than 1 value in a group where status = 1 thann a better option is to use match which will return NA for 0 value and return the 1st death value for more than 1 value.如果在status = 1的组中有 0 个或超过 1 个值,则更好的选择是使用match ,它将为 0 值返回NA并为超过 1 个值返回第一个death值。

df %>%
  group_by(stratum) %>%
  mutate(weights = death[match(1, status)]) %>%
  ungroup

#  status stratum death weights
#   <dbl>   <dbl> <int>   <int>
#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM