创建新列的变异和if else函数

huangapple go评论88阅读模式
英文:

Mutate and if else function to create new column

问题

以下是翻译好的部分:

  1. 这可能是一个非常简单的问题,但到目前为止我失败了。
  2. 我的数据集看起来像这样:
  3. | Duration | Unit |
  4. | -------- | -------- |
  5. | 1 | |
  6. | 3 | |
  7. | 5 | |
  8. 我想要做的是根据单位创建一个新的列,用于表示天数。所以3个月应该是90天,5周应该是35天。如果单位是天,那么天的值应该直接放入我的新列中,无需任何计算。
  9. 所以结果应该是这样的:
  10. | Duration | Unit | Duration_calculated |
  11. | -------- | -------- | ------- |
  12. | 1 | | 1 |
  13. | 3 | | 90 |
  14. | 5 | | 35 |
  15. 这是示例数据集:
  16. ```R
  17. Duration <- c(1,3,5)
  18. Unit <- c("天", "月", "周")
  19. dataset <- data.frame(Duration, Unit)

我尝试了mutate和if else函数的组合,但对我来说没有成功。

  1. <details>
  2. <summary>英文:</summary>
  3. It may be a very easy question, but so far I failed.
  4. My dataset looks like this
  5. | Duration | Unit |
  6. | -------- | -------- |
  7. | 1 | day |
  8. | 3 | month |
  9. | 5 | weeks |
  10. What I want to do is to create a new column for the number of days depending on the unit. So 3 months should be 90 days, 5 weeks should be 35 days. And in case of unit is day, the value of day should be placed in my new column without any calculation.
  11. So the result should look like this
  12. | Duration | Unit | Duration_calculated |
  13. | -------- | -------- | ------- |
  14. | 1 | day | 1 |
  15. | 3 | month | 90 |
  16. | 5 | weeks | 35 |
  17. Here is the exmaple dataset

Duration <- c(1,3,5)
Unit <- c("day", "month", "weeks")
dataset <- data.frame(Duration, Unit)

  1. I&#39;ve tried a combination of mutate and if else function, but it worked not out for me.
  2. </details>
  3. # 答案1
  4. **得分**: 3
  5. 生成一个查找向量,然后进行匹配:
  6. ```R
  7. dataset <- data.frame(Duration = c(1, 3, 5),
  8. Unit = c("day", "month", "week"))
  9. lookup <- setNames(c(1, 7, 30.437), c("day", "week", "month"))
  10. dataset$Duration_calculated <- dataset$Duration * lookup[dataset$Unit]
  11. dataset
  12. # Duration Unit Duration_calculated
  13. # 1 1 day 1.000
  14. # 2 3 month 91.311
  15. # 3 5 week 35.000
英文:

Make a lookup vector, then match:

  1. dataset &lt;- data.frame(Duration = c(1, 3, 5),
  2. Unit = c(&quot;day&quot;, &quot;month&quot;, &quot;week&quot;))
  3. lookup &lt;- setNames(c(1, 7, 30.437), c(&quot;day&quot;, &quot;week&quot;, &quot;month&quot;))
  4. dataset$Duration_calculated &lt;- dataset$Duration * lookup[ dataset$Unit ]
  5. dataset
  6. # Duration Unit Duration_calculated
  7. # 1 1 day 1.000
  8. # 2 3 month 91.311
  9. # 3 5 week 35.000

答案2

得分: 1

我们可以使用 case_when()

  1. library(dplyr)
  2. df %>%
  3. mutate(Duration_calculated = case_when(
  4. Unit == "day" ~ Duration,
  5. Unit == "month" ~ Duration * 30,
  6. Unit == "weeks" ~ Duration * 7
  7. ))

或者如果您坚持使用 ifelseif_else,我们可以使用嵌套的 if_else() 语句:

  1. df %>%
  2. mutate(Duration_calculated = if_else(
  3. Unit == "day", Duration,
  4. if_else(Unit == "month", Duration * 30,
  5. if_else(Unit == "weeks", Duration * 7, NA_real_)
  6. )
  7. ))
英文:

We could use case_when():

  1. library(dplyr)
  2. df %&gt;%
  3. mutate(Duration_calculated = case_when(
  4. Unit == &quot;day&quot; ~ Duration,
  5. Unit == &quot;month&quot; ~ Duration * 30,
  6. Unit == &quot;weeks&quot; ~ Duration * 7
  7. ))
  8. Duration Unit Duration_calculated
  9. 1 1 day 1
  10. 2 3 month 90
  11. 3 5 weeks 35

Or if you stick on ifelse or if_else we could use nested if_else() statement:

  1. df %&gt;%
  2. mutate(Duration_calculated = if_else(
  3. Unit == &quot;day&quot;, Duration,
  4. if_else(Unit == &quot;month&quot;, Duration * 30,
  5. if_else(Unit == &quot;weeks&quot;, Duration * 7, NA_real_)
  6. )
  7. ))

huangapple
  • 本文由 发表于 2023年7月20日 16:31:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定