根据R中的其他变量创建新变量

发布于 2025-02-03 04:04:51 字数 596 浏览 4 评论 0原文

与R一起工作,我正在尝试根据数据框架中的3个现有变量创建一个变量。示例:

n <- 20
dat <- data.frame(id=1:n,
                  group=rep(LETTERS[1:5]),
                  type=factor(paste("type", 1:2)))

新变量“模型”的值应基于这3:

If "type" = 1 then "model"= apple 
If "type" = 2 and "group" A or B, then "model"=cherry 
If "type" = 2 and "group" C and "id" 1-4, then"model"= strawberry 
If "type" = 2 and "group" C and "id" 5-8, then"model"= melon 
If "type" = 2 and "group" C and "id" >9, then "model"=pineapple

我尝试了ifelse,但我在条件上挣扎。

如何解决这个问题?

Working with R, I am trying to create a variable, based on 3 existing variables in a data frame. Example:

n <- 20
dat <- data.frame(id=1:n,
                  group=rep(LETTERS[1:5]),
                  type=factor(paste("type", 1:2)))

The values of the new variable "model" should be based on those 3:

If "type" = 1 then "model"= apple 
If "type" = 2 and "group" A or B, then "model"=cherry 
If "type" = 2 and "group" C and "id" 1-4, then"model"= strawberry 
If "type" = 2 and "group" C and "id" 5-8, then"model"= melon 
If "type" = 2 and "group" C and "id" >9, then "model"=pineapple

I have tried with ifelse, but I struggle with the conditions.

How to solve this?

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

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

发布评论

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

评论(2

擦肩而过的背影 2025-02-10 04:04:51

您可以从Tidyverse使用case_when

library(tidyverse)
n <- 20
dat <- data.frame(id=1:n,
                  group=rep(LETTERS[1:5]),
                  type=factor(paste("type", 1:2)))

dat %>% 
  mutate(
    model = case_when(
      type == "type 1" ~ "apple",
      type == "type 2" & group %in% c("A", "B") ~ "cherry",
      type == "type 2" & group == "C" & id %in% c(1:4) ~ "strawberry",
      type == "type 2" & group == "C" & id %in% c(5:8) ~ "melon",
      type == "type 2" & group == "C" & id >9 ~ "pineapple"
    )
  )

You can use case_when from the tidyverse:

library(tidyverse)
n <- 20
dat <- data.frame(id=1:n,
                  group=rep(LETTERS[1:5]),
                  type=factor(paste("type", 1:2)))

dat %>% 
  mutate(
    model = case_when(
      type == "type 1" ~ "apple",
      type == "type 2" & group %in% c("A", "B") ~ "cherry",
      type == "type 2" & group == "C" & id %in% c(1:4) ~ "strawberry",
      type == "type 2" & group == "C" & id %in% c(5:8) ~ "melon",
      type == "type 2" & group == "C" & id >9 ~ "pineapple"
    )
  )
俏︾媚 2025-02-10 04:04:51

嵌套ifelse选项:

dat$color <- with(dat, ifelse(type == "type 1", "apple",
                       ifelse(type == "type 2" & group %in% c("A", "B"), "cherry",
                       ifelse(type == "type 2" & group == "C" & id %in% (1:4), "strawberry",
                       ifelse(type == "type 2" & group == "C" & id %in% (5:8), "melon",
                       ifelse(type == "type 2" & group == "C" & id >= 9, "pineapple", NA))))))

输出:

   id group   type     color
1   1     A type 1     apple
2   2     B type 2    cherry
3   3     C type 1     apple
4   4     D type 2      <NA>
5   5     E type 1     apple
6   6     A type 2    cherry
7   7     B type 1     apple
8   8     C type 2     melon
9   9     D type 1     apple
10 10     E type 2      <NA>
11 11     A type 1     apple
12 12     B type 2    cherry
13 13     C type 1     apple
14 14     D type 2      <NA>
15 15     E type 1     apple
16 16     A type 2    cherry
17 17     B type 1     apple
18 18     C type 2 pineapple
19 19     D type 1     apple
20 20     E type 2      <NA>

Nested ifelse option:

dat$color <- with(dat, ifelse(type == "type 1", "apple",
                       ifelse(type == "type 2" & group %in% c("A", "B"), "cherry",
                       ifelse(type == "type 2" & group == "C" & id %in% (1:4), "strawberry",
                       ifelse(type == "type 2" & group == "C" & id %in% (5:8), "melon",
                       ifelse(type == "type 2" & group == "C" & id >= 9, "pineapple", NA))))))

Output:

   id group   type     color
1   1     A type 1     apple
2   2     B type 2    cherry
3   3     C type 1     apple
4   4     D type 2      <NA>
5   5     E type 1     apple
6   6     A type 2    cherry
7   7     B type 1     apple
8   8     C type 2     melon
9   9     D type 1     apple
10 10     E type 2      <NA>
11 11     A type 1     apple
12 12     B type 2    cherry
13 13     C type 1     apple
14 14     D type 2      <NA>
15 15     E type 1     apple
16 16     A type 2    cherry
17 17     B type 1     apple
18 18     C type 2 pineapple
19 19     D type 1     apple
20 20     E type 2      <NA>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文