如何创建基于另一个变量的值递增1的变量?

时间:2018-12-28 15:31:50

标签: r

说我有一个像这样的数据集:

id <- rep(1, 9)
start_over <- c(rep(NA, 3), "yes", NA, "yes", rep(NA, 3))
dat <- data.frame(id, start_over)

    id  start_over
1   1   NA
2   1   NA 
3   1   NA
4   1   yes
5   1   NA
6   1   yes
7   1   NA
8   1   NA
9   1   NA

我将如何创建一个新变量,每次start_over为“ yes”时该变量将递增1。

    id  start_over   assignment
1   1   NA           1
2   1   NA           1
3   1   NA           1
4   1   yes          2
5   1   NA           2
6   1   yes          3
7   1   NA           3
8   1   NA           3
9   1   NA           3

3 个答案:

答案 0 :(得分:5)

我的评论有一个小改进:

* {
  margin: 0;
  padding: 0;
}
html, body {
  height: 100%;
}
.mainCountainer {
  min-height: 100%;
  width: 70%;
  margin: 0 auto;
}
.mainCountainerHead {
  background-color: aqua;
  height: auto;
}
.mainCountainerHeadLogo {
  height: 100px;
  width: 20%;
  background-color: blue;
  float: left;
  position: relative;
  overflow: hidden;
}
.mainCountainerHeadLogo img {
  position: absolute;
  width: 100%;
  height: auto;
  top: -50%;
  right: -50%;
  bottom: -50%;
  left: -50%;
  margin: auto
}
.mainCountainerHeadTitle{
  margin-left: 20%;
  width: 80%;
  height: auto;
  text-align: center;
  padding-top: 3%;
}
.mainCountainerHeadMenu{
  margin-left: 20%;
  text-align: center;
  background-color: orange;
  width: 80%;
  height: auto;
  padding-top: 2%;
  text-align: center;
}
.mainLink {
  display: inline-block;
  padding: 5px;
}
.mainLinkButton {
  width: 90px;
  height: 30px;
  background-color: green;
  font-size: 16px;
  border: none;
  color: white;
  padding: 5px;
}
.mainLinkButton:hover {
  background-color: darkgreen;
}
.mainLinkDropdown {
  position: relative;
  display: inline-block;
  padding: 5px;
}
.dropdownContent {
  display: none;
  position: absolute;
  min-height: 30px;
  min-width: 130px;
  text-align: left;
  background-color: #f1f1f1;
  z-index: 10;
}
.dropdownContent a {
  display: block;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
}
.mainLinkDropdown:hover .dropdownContent{
  display: block;
}
.dropdownContent a:hover{
  background-color: #ddd;
}

给出:

<div class="mainCountainer">
 <div class="mainCountainerHead">
    <div class="mainCountainerHeadLogo">
      <img src="https://i.ibb.co/cYzWJFM/logo-Copy.jpg" title="logo" />
    </div>
    <div class="mainCountainerHeadTitle">
      <h4>Welcome aboard!</h4>
    </div>
    <div class="mainCountainerHeadMenu">
      <div class="mainLink">
        <button class="mainLinkButton">Main</button>
      </div>
      <div class="mainLinkDropdown">
        <button class="mainLinkButton">Dropdown</button>
        <div class="dropdownContent">
          <a href="">Link 1</a>
          <a href="">Link 2</a>
          <a href="">Link 3</a>
        </div>
      </div>
      <div class="mainLink">
        <button class="mainLinkButton">Contacts</button>
      </div>
    </div>
  </div>
  </div>

答案 1 :(得分:2)

我们可以使用cumsum函数:

cumsum(dat$start_over == "yes" & !is.na(dat$start_over)) + 1
# [1] 1 1 1 2 2 3 3 3 3

我们正在检查start_over是否等于“是”而不是NA。如果满足这些条件,我们将返回1,否则将返回0。我们需要在1上添加cumsum,否则分配将从0开始。

dat$assignment <- cumsum(dat$start_over == "yes" & !is.na(dat$start_over)) + 1

#   id start_over assignment
# 1  1       <NA>          1
# 2  1       <NA>          1
# 3  1       <NA>          1
# 4  1        yes          2
# 5  1       <NA>          2
# 6  1        yes          3
# 7  1       <NA>          3
# 8  1       <NA>          3
# 9  1       <NA>          3

答案 2 :(得分:1)

NA可以仅使用is.na()函数,然后使用cumsum()布尔值来标识。

library(dplyr)
dat %>% mutate(x = cumsum(!is.na(start_over)) + 1)

#   id start_over x
# 1  1       <NA> 1
# 2  1       <NA> 1
# 3  1       <NA> 1
# 4  1        yes 2
# 5  1       <NA> 2
# 6  1        yes 3
# 7  1       <NA> 3
# 8  1       <NA> 3
# 9  1       <NA> 3