Jie
Jie

Reputation: 29

How to assign same value to a new variable by group condition on another variable?

I have tried many times to solve the problem in the title. Here is the code I used, I hope someone can help. Thank you.

This is the dataframe in the beginning:

  df = data.frame(ID = c(1,1,1,2,2,2,3,3,3), 
       Diagnosis=c("NEG","NEG","POS","NEG","NEG","NEG","NEG","POS","POS"))

I want to create a new variable "Diagnosis_approach" = 'a' for the group with the same ID if 'Diagnosis' ever equals to 1 and "Diagnosis_approach" = 'b' for the group with the same ID if 'Diagnosis'never equals to 1. This is the data frame I want to get:

df = data.frame(ID = c(1,1,1,2,2,2,3,3,3), 
      Diagnosis=c("NEG","NEG","POS","NEG","NEG","NEG","NEG","POS","POS"),
      Diagnosis_approach=c("a","a","a","b","b","b","a","a","a"))

I used the following code, but the results seem not correct. The value of 'Diagnosis' is supposed to be the same for the same ID, but this code did not get the intended results.

df<-df %>% 
   group_by(ID) %>% 
    mutate(Diagnosis_approach=case_when(Diagnosis %in%"POS"~"a",Diagnosis 
        %in% "NEG" ~ "b"))

Upvotes: 0

Views: 707

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101257

A base R option using ave

transform(
  df,
  Diagnosis_approach = c("b", "a")[1 + ave(Diagnosis == "POS", ID, FUN = any)]
)

gives

  ID Diagnosis Diagnosis_approach
1  1       NEG                  a
2  1       NEG                  a
3  1       POS                  a
4  2       NEG                  b
5  2       NEG                  b
6  2       NEG                  b
7  3       NEG                  a
8  3       POS                  a
9  3       POS                  a

If you want using tidyverse, try below

df %>%
  group_by(ID) %>%
  mutate(
    Diagnosis_approach = case_when(
      "POS" %in% Diagnosis ~ "a",
      TRUE ~ "b"
    )
  )

Upvotes: 0

Related Questions