英文:
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'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 <- 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
答案2
得分: 1
我们可以使用 case_when()
:
library(dplyr)
df %>%
mutate(Duration_calculated = case_when(
Unit == "day" ~ Duration,
Unit == "month" ~ Duration * 30,
Unit == "weeks" ~ Duration * 7
))
或者如果您坚持使用 ifelse
或 if_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 %>%
mutate(Duration_calculated = case_when(
Unit == "day" ~ Duration,
Unit == "month" ~ Duration * 30,
Unit == "weeks" ~ 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 %>%
mutate(Duration_calculated = if_else(
Unit == "day", Duration,
if_else(Unit == "month", Duration * 30,
if_else(Unit == "weeks", Duration * 7, NA_real_)
)
))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论