4

I have three categorical variables i.e. stroke, MI and BP with values of 0 = yes and 1= No. I want to merge them to make a new variable "cvd" out of these three variables where each row with 0 gets 0 values in new cardiovascular variable. For example:

Stroke  MI  BP  CVD
0       1    1   0
1       1    1   1
1       1    0   0

I tried the following code but this is not what i want

transform(koratest, cvd=paste(stroke,MI, BP))

Can someone please help what could be the script for this?

Best,

Thank you for all the solutions. What to do if there is missing values in any of the values to be merged. I want missing values to be labelled as 1 but if there is 0 with missing value, i want cvd variable to have value of 1. For example:

 Stroke  MI  BP  CVD
0       1    1   0
1       NA   NA  1
0       NA   1   0

How could i achieve such output?

0

8 Answers 8

2

Try,

(rowSums(df) == ncol(df)) * 1
#[1] 0 1 0
Sign up to request clarification or add additional context in comments.

2 Comments

this looks good. Thank you. But my dataset has a list of other variables as well. How should i select only these three variable in the script and where should i define the name of new variable
If they are the first 3 on your data frame then df[1:3] or the index they are found on your df
1

Another way:

library(dplyr)

df <- data.frame(Stroke = c(0,1,1),
                   MI = c(1,1,1),
                   BP = c(1,1,0))

df %>% 
  rowwise() %>% 
  mutate(
    CVD = min(Stroke, MI, BP) 
  ) %>% 
  ungroup()

#> # A tibble: 3 × 4
#>   Stroke    MI    BP   CVD
#>    <dbl> <dbl> <dbl> <dbl>
#> 1      0     1     1     0
#> 2      1     1     1     1
#> 3      1     1     0     0

Created on 2022-07-11 by the reprex package (v2.0.1)

Comments

1

Don't know how you arrange your variables. If they are separted vectors, this should work:

Stroke = c(0,1,1)
MI = c(1,1,1)
BP = c(1,1,0)
CVD = as.numeric(Stroke & MI & BP)

If a data.frame:

df$CVD = with(df, as.numeric(Stroke & MI & BP)

Or the solutions mentioned by others.

Comments

1

Try this using dplyr rowwise function

library(dplyr)

df |> rowwise() |> mutate(CVD = if(all(c_across() == 1)) 1 else 0) |> ungroup()
  • output
# A tibble: 3 × 4
# Rowwise: 
  Stroke    MI    BP   CVD
   <int> <int> <int> <dbl>
1      0     1     1     0
2      1     1     1     1
3      1     1     0     0

1 Comment

You may want to add a ungroup to make it usual tbl_df object.
0

Maybe this:

library(tidyverse)

Data <- data.frame(Stroke = c(0,1,1),
                   MI = c(1,1,1),
                   BP = c(1,1,0))

Data <- Data %>% 
  mutate(CVD = if_else(Stroke == 1 &MI == 1 & BP == 1, 1, 0))

Comments

0

base R option:

df$CVD <- apply(df,2, function(x) !any(0 %in% x)) + 0
df

Output:

  Stroke MI BP CVD
1      0  1  1   0
2      1  1  1   1
3      1  1  0   0

1 Comment

another aspect to this question: How i deal if i have any missing values in the variables to be merged. Detail has been edited in the question
0

Using rowSums in cbind detects that dat is a data frame and creates such.

cbind(dat, CVD=+(rowSums(dat[c('Stroke', 'MI', 'BP')]) == 3))
#   Stroke MI BP CVD
# 1      0  1  1   0
# 2      1  1  1   1
# 3      1  1  0   0

If you only have these columns, it simplifies to:

cbind(dat, CVD=+(rowSums(dat) == 3))

Data:

dat <- structure(list(Stroke = c(0L, 1L, 1L), MI = c(1L, 1L, 1L), BP = c(1L, 
1L, 0L)), class = "data.frame", row.names = c(NA, -3L))

Comments

0

Another way to solves your problem:

df$CVD = with(df, pmin(Stroke, MI, BP)) 

  Stroke MI BP CVD
1      0  1  1   0
2      1  1  1   1
3      1  1  0   0

# or
library(data.table)

setDT(df)[, CVD := pmin(Stroke, MI, BP)]

# or
library(dplyr)

df = df %>% 
  mutate(CVD = pmin(Stroke, MI, BP))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.