简体   繁体   中英

How to add column based on other column with multiple condition

I have a dataframe. I want to write condition for creating a column which depends on other column. Here it is:

tab <- tibble::tribble(
  ~dataset_id,  ~type,
     "Site4H",      268,
     "Site4D",      479,
     "SIte8H",      345,
     "Site8D",      567,
     "Site8K",      blond507
  )
library(dplyr)
tab %>%
  mutate(state = case_when(
    endsWith(dataset_id, "H") ~ "healthy",
    endsWith(dataset_id, "D") ~ "disease",
    TRUE                      ~ NA_character_
  ))

As you see column state equals healthy if value in column dataset_id ends with H. But i want it to be healthy for two cases: when value in column dataset_id ends with H and when value in column "type" starts with "blond". How could i do that? I need to use these exact functions, so solution with another libraries is not good.

Desired results is:

dataset_id   type          state  
 Site4H       268         healthy
 Site4D       479         disease
 SIte8H       345         healthy
 Site8D       567         disease
 Site8K       blond507    healthy  
tab <- tibble::tribble(
  ~dataset_id,  ~type,
  "Site4H",      "268",
  "Site4D",      "479",
  "SIte8H",      "345",
  "Site8D",      "567",
  "Site8K",      "blond507"
)

tab %>%
  mutate(state = case_when(
    endsWith(dataset_id, "H") | startsWith(type, "blond") ~ "healthy",
    endsWith(dataset_id, "D") ~ "disease",
    TRUE                      ~ NA_character_
  ))

So you can simply add the type restriction to your case_when code which gives:

# A tibble: 5 x 3
  dataset_id type     state  
  <chr>      <chr>    <chr>  
1 Site4H     268      healthy
2 Site4D     479      disease
3 SIte8H     345      healthy
4 Site8D     567      disease
5 Site8K     blond507 healthy

Two notes:

  1. I'm assuming you want to code "healthy" if id ends with "H" OR if type is "blond507", although you said in your post it should be "and".
  2. You edited your code so that type 507 is now "blond507". Note that this won't work, because the other values are integers or doubles and "blond507" would be a character. So you need to adjust your input code for tab (see start of my post).

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