简体   繁体   中英

Create a new column based on multiple conditions of one string column in R

I have a dataset as follows:

df <- structure(list(city = structure(c(1L, 5L, 6L, 2L, 4L, 7L, 9L, 
8L, 3L), .Label = c("bj", "cq", "cs", "nj", "sh", "tj", "wh", 
"xa", "xm"), class = "factor"), area = c(1580.86, 1927.95, 532.24, 
613.09, 1459.86, 1024.58, 684.63, NA, 708.35), price = c(9.51, 
94.42, 10.77, 8.58, 38.07, 94.14, 30.43, 45.73, 41.39)), class = "data.frame", row.names = c(NA, 
-9L))

Now I want to create a new columns level based on the following conditions:

for bj and sh , the values of level are a ; for cq and tj , the values of level are b ; for the others cities, the values are c .

How could I do that in R? Thanks.

Using dplyr::case_when and stringr::str_detect you could do this:

library(dplyr)
library(stringr)

df %>% 
  mutate(level = case_when(str_detect(city, "bj|sh" ) ~ "a",
                           str_detect(city, "cq|tj" ) ~ "b",
                           TRUE ~ "c"))
#>   city    area price level
#> 1   bj 1580.86  9.51     a
#> 2   sh 1927.95 94.42     a
#> 3   tj  532.24 10.77     b
#> 4   cq  613.09  8.58     b
#> 5   nj 1459.86 38.07     c
#> 6   wh 1024.58 94.14     c
#> 7   xm  684.63 30.43     c
#> 8   xa      NA 45.73     c
#> 9   cs  708.35 41.39     c

Created on 2020-06-26 by the reprex package (v0.3.0)

library(tidyverse)
df %>% mutate(level = ifelse(city %in% c("bj", "sh"), "a", 
                             ifelse(city %in% c("cq", "tj"), "b", "c")))


  city    area price level
1   bj 1580.86  9.51     a
2   sh 1927.95 94.42     a
3   tj  532.24 10.77     b
4   cq  613.09  8.58     b
5   nj 1459.86 38.07     c
6   wh 1024.58 94.14     c
7   xm  684.63 30.43     c
8   xa      NA 45.73     c
9   cs  708.35 41.39     c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM