如何根据R中另一列的特定值正确更改级别值?

发布于 2025-01-25 21:44:35 字数 721 浏览 4 评论 0原文

我想根据同一列的特定值和另一列的特定值更改/重新编码列的级别。例如,让我们使用ggplot2 ::钻石。在这种情况下,我想在 剪切 列中更改“ premium”的价值,如果列 color 是“ d”,如果列 color 是“ j”,则将“ prem”的值更改为“惊人”。这是我的尝试:

df <- ggplot2::diamonds
unique(df$cut) #to look at the initial values


df$cut <- with(df,ifelse(cut == "Premium" & color == "D", "Perfect", 
                      ifelse(cut== "Premium" & color == "J","Amazing", cut)))

问题是,随后查看剪切列时,其他值也已更改。

unique(df$cut)

[1] "5"       "4"       "2"       "3"       "1"       "Perfect" "Amazing"

有人可以告诉我我在这里做错了什么吗?如果除了我尝试这样做之外还有其他方法,我也很感谢您看到这一点!

I want to change/recode the levels for a column based on a specific value of the same column and another column. As an example, let's use ggplot2::diamonds. In this scenario, I want to change the value of "Premium" in the cut column to "Perfect" if the column color is "D" and change the value of "Premium" to "Amazing" if column color is "J". This is my attempt:

df <- ggplot2::diamonds
unique(df$cut) #to look at the initial values


df$cut <- with(df,ifelse(cut == "Premium" & color == "D", "Perfect", 
                      ifelse(cut== "Premium" & color == "J","Amazing", cut)))

The issue is that when looking at the cut column afterwards, the other values have also been changed.

unique(df$cut)

[1] "5"       "4"       "2"       "3"       "1"       "Perfect" "Amazing"

Can someone please tell me what I am doing wrong here? If there are other ways than how I attempted to do this, I would also appreciate seeing that as well!

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

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

发布评论

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

评论(2

稚然 2025-02-01 21:44:35

使用case_when()

library(dplyr)
df <- df %>% 
  mutate(cut = 
           as.factor(case_when(
    (cut == "Premium" & color == "D") ~ "Perfect",
    (cut == "Premium" & color == "J") ~ "Amazing",
    TRUE  ~ as.character(cut))
  )
  ) 

unique(df$cut)

输出:

[1] Ideal     Premium   Good      Very Good Fair      Perfect   Amazing  
Levels: Amazing Fair Good Ideal Perfect Premium Very Good

Using case_when():

library(dplyr)
df <- df %>% 
  mutate(cut = 
           as.factor(case_when(
    (cut == "Premium" & color == "D") ~ "Perfect",
    (cut == "Premium" & color == "J") ~ "Amazing",
    TRUE  ~ as.character(cut))
  )
  ) 

unique(df$cut)

Output:

[1] Ideal     Premium   Good      Very Good Fair      Perfect   Amazing  
Levels: Amazing Fair Good Ideal Perfect Premium Very Good
漆黑的白昼 2025-02-01 21:44:35

我经常使用此base r方法:

df$cut <- as.character(df$cut)

df$cut[df$color == "D" & df$cut == "Premium" ] <-  "Perfect"
df$cut[df$color == "J" & df$cut == "Premium" ] <-  "Amazing"

df$cut <- as.ordered(df$cut)

但是您必须先将因子转换为字符,否则您会遇到错误。

I often use this base R method:

df$cut <- as.character(df$cut)

df$cut[df$color == "D" & df$cut == "Premium" ] <-  "Perfect"
df$cut[df$color == "J" & df$cut == "Premium" ] <-  "Amazing"

df$cut <- as.ordered(df$cut)

But you have to turn your factor to character first, or you will get an error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文