创建具有特定元素的列

发布于 01-24 02:48 字数 1036 浏览 5 评论 0原文

我有以下脚本:

library(lubridate)
library(tidyverse)

date_vec <- seq(dmy("01-11-2020"), dmy("15-07-2022"), by = "day")

df <- tibble(date = date_vec, description = NA)

limpmerg <- dmy("04-11-2020", "16-11-2020", "25-11-2020", "03-12-2020", 
                "20-12-2020", "28-12-2020", "08-01-2021", "21-01-2021", 
                "28-01-2021", "24-02-2021", "06-03-2021", "11-03-2021", 
                "22-03-2021", "30-03-2021", "04-04-2021", "19-04-2021", 
                "28-04-2021", "25-05-2021", "08-06-2021", "15-06-2021", 
                "21-06-2021", "24-06-2021", "30-06-2021", "09-07-2021", 
                "15-07-2021")

falhequip <- dmy("20-11-2020", "21-11-2020", "23-11-2020", "24-11-2020",
                 "25-11-2020", "04-01-2021", "05-01-2021", "06-01-2021", 
                 "07-01-2021", "24-01-2021", "25-01-2021", "26-01-2021")

我会添加df的列描述,以向量limpmerg中的日期为“清洁”,在vector <代码> falhequip 。在其他日期,我会添加文本“收集”。

我该怎么做?

谢谢

I have the following script:

library(lubridate)
library(tidyverse)

date_vec <- seq(dmy("01-11-2020"), dmy("15-07-2022"), by = "day")

df <- tibble(date = date_vec, description = NA)

limpmerg <- dmy("04-11-2020", "16-11-2020", "25-11-2020", "03-12-2020", 
                "20-12-2020", "28-12-2020", "08-01-2021", "21-01-2021", 
                "28-01-2021", "24-02-2021", "06-03-2021", "11-03-2021", 
                "22-03-2021", "30-03-2021", "04-04-2021", "19-04-2021", 
                "28-04-2021", "25-05-2021", "08-06-2021", "15-06-2021", 
                "21-06-2021", "24-06-2021", "30-06-2021", "09-07-2021", 
                "15-07-2021")

falhequip <- dmy("20-11-2020", "21-11-2020", "23-11-2020", "24-11-2020",
                 "25-11-2020", "04-01-2021", "05-01-2021", "06-01-2021", 
                 "07-01-2021", "24-01-2021", "25-01-2021", "26-01-2021")

I would add in column description, of df a text "clean" to dates in vector limpmerg, a text "failing" to dates in vector falhequip. To the other dates, I would add text "collect".

How can I do this?

Thank's

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗琴音2025-01-31 02:48:44

我们可以使用case_when

library(dplyr)
df1 <- df %>%
  mutate(description = case_when(date %in% limpmerg ~ "clean", 
                                date %in% falhequip ~ "failing",
                                 TRUE ~ "collect"))

We may use case_when

library(dplyr)
df1 <- df %>%
  mutate(description = case_when(date %in% limpmerg ~ "clean", 
                                date %in% falhequip ~ "failing",
                                 TRUE ~ "collect"))
九厘米的零°2025-01-31 02:48:44

在基本r中,您可以使用简单的嵌套ifelse语句进行此操作,尽管在您的情况下,@akrun的case_when方法可能是首选的:

df$description <- ifelse(df$date %in% limpmerg, "clean",
                         ifelse(df$date %in% falhequip, "failing", "collect"))

输出:输出:

# date       description
# <date>     <chr>      
#   1 2020-11-01 collect    
# 2 2020-11-02 collect    
# 3 2020-11-03 collect    
# 4 2020-11-04 clean      
# 5 2020-11-05 collect    
# 6 2020-11-06 collect    
# 7 2020-11-07 collect 

In base R you can do this using a simple nested ifelse statement, though the case_when approach by @akrun is likely preferred in your situation:

df$description <- ifelse(df$date %in% limpmerg, "clean",
                         ifelse(df$date %in% falhequip, "failing", "collect"))

Output:

# date       description
# <date>     <chr>      
#   1 2020-11-01 collect    
# 2 2020-11-02 collect    
# 3 2020-11-03 collect    
# 4 2020-11-04 clean      
# 5 2020-11-05 collect    
# 6 2020-11-06 collect    
# 7 2020-11-07 collect 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文