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

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

Mutate and if else function to create new column

问题

以下是翻译好的部分:

这可能是一个非常简单的问题,但到目前为止我失败了。

我的数据集看起来像这样:

| Duration  | Unit |
| -------- | -------- |
| 1   | 天   |
| 3   | 月   |
| 5   | 周 |

我想要做的是根据单位创建一个新的列,用于表示天数。所以3个月应该是90天,5周应该是35天。如果单位是天,那么天的值应该直接放入我的新列中,无需任何计算。

所以结果应该是这样的:

| Duration | Unit | Duration_calculated |
| -------- | -------- | ------- |
| 1   | 天   | 1 |
| 3   | 月   | 90 |
| 5   | 周  | 35 |

这是示例数据集:

```R
Duration <- c(1,3,5)    
Unit <- c("天", "月", "周")     
dataset <- data.frame(Duration, Unit)    

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


<details>
<summary>英文:</summary>

It may be a very easy question, but so far I failed. 

My dataset looks like this


| Duration  | Unit |
| -------- | -------- |
| 1   | day   |
| 3   | month   |
| 5   | weeks |

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.

So the result should look like this




| Duration | Unit | Duration_calculated |
| -------- | -------- | ------- |
| 1   | day   | 1 |
| 3   | month   | 90 |
| 5   | weeks  | 35 |

 

Here is the exmaple dataset


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



I&#39;ve tried a combination of mutate and if else function, but it worked not out for me.





</details>


# 答案1
**得分**: 3

生成一个查找向量,然后进行匹配:

```R
dataset <- data.frame(Duration = c(1, 3, 5),
                      Unit = c("day", "month", "week"))

lookup <- setNames(c(1, 7, 30.437), c("day", "week", "month"))

dataset$Duration_calculated <- dataset$Duration * lookup[dataset$Unit]

dataset
#   Duration  Unit Duration_calculated
# 1        1   day               1.000
# 2        3 month              91.311
# 3        5  week              35.000
英文:

Make a lookup vector, then match:

dataset &lt;- data.frame(Duration = c(1, 3, 5),
                      Unit = c(&quot;day&quot;, &quot;month&quot;, &quot;week&quot;))

lookup &lt;- setNames(c(1, 7, 30.437), c(&quot;day&quot;, &quot;week&quot;, &quot;month&quot;))

dataset$Duration_calculated &lt;- dataset$Duration * lookup[ dataset$Unit ]

dataset
#   Duration  Unit Duration_calculated
# 1        1   day               1.000
# 2        3 month              91.311
# 3        5  week              35.000

答案2

得分: 1

我们可以使用 case_when()

library(dplyr)

df %>%
  mutate(Duration_calculated = case_when(
    Unit == "day" ~ Duration,
    Unit == "month" ~ Duration * 30,
    Unit == "weeks" ~ Duration * 7
  ))

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

df %>%
  mutate(Duration_calculated = if_else(
    Unit == "day", Duration,
    if_else(Unit == "month", Duration * 30,
            if_else(Unit == "weeks", Duration * 7, NA_real_)
    )
  ))
英文:

We could use case_when():

library(dplyr)

df %&gt;%
  mutate(Duration_calculated = case_when(
    Unit == &quot;day&quot; ~ Duration,
    Unit == &quot;month&quot; ~ Duration * 30,
    Unit == &quot;weeks&quot; ~ Duration * 7
  ))

  Duration  Unit Duration_calculated
1        1   day                   1
2        3 month                  90
3        5 weeks                  35

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

df %&gt;% 
  mutate(Duration_calculated = if_else(
  Unit == &quot;day&quot;, Duration,
  if_else(Unit == &quot;month&quot;, Duration * 30,
          if_else(Unit == &quot;weeks&quot;, Duration * 7, NA_real_)
  )
))

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:

确定