簡體   English   中英

有沒有辦法在 R 中使用 dplyr 根據另一個列的值創建一個新列?

[英]Is there a way to create a new column based on the values of another one using dplyr in R?

我一直在使用基礎 R 但我想使用dplyr 這就是我一直在做的事情:

data$newvariable <- 0
data$newvariable[data$oldvariable=="happy"] <- "good"
data$newvariable[data$oldvariable=="unhappy"] <- "bad"
data$newvariable[data$oldvariable=="depressed"] <- "super_bad"

如果 oldvariable 是一個因素,並且您不介意 newvariable 是一個因素:

library(dplyr)

set.seed(111)
data = data.frame(
oldvariable=sample(c("happy","unhappy","depressed"),10,replace=TRUE))

data %>% mutate(newvariable=recode_factor(oldvariable,
"happy"="good","unhappy"="bad","depressed"="super_bad"))


   oldvariable newvariable
1      unhappy         bad
2    depressed   super_bad
3    depressed   super_bad
4    depressed   super_bad
5        happy        good
6    depressed   super_bad
7        happy        good
8    depressed   super_bad
9      unhappy         bad
10       happy        good

dplyr ,我們可以使用case_when新的值賦給newvariable基於oldvariable

library(dplyr)

data = data.frame(
  oldvariable = c("happy", "unhappy", "depressed")
)

data %>%
  mutate(newvariable = case_when(
    oldvariable == "happy" ~ "good",
    oldvariable == "unhappy" ~ "bad",
    oldvariable == "depressed" ~ "super_bad"
  ))
#>   oldvariable newvariable
#> 1       happy        good
#> 2     unhappy         bad
#> 3   depressed   super_bad

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM