In response to my previous question here:
Merging binary variables in R to a new variable
I used the following code to merge categorical variables and it worked:
koratest<-koratest%>%
rowwise() %>%
mutate(
CVD = min(ptmi,ptschl,pthyact, ptap )
) %>%
ungroup()
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 should i edit the above mentioned code in this case?
CodePudding user response:
You could first replace the NA with 1 and then apply
the same function:
df[is.na(df)] <- 1
df$CVD <- apply(df,1, function(x) !any(0 %in% x)) 0
df
Output:
Stroke MI BP CVD
1 0 1 1 0
2 1 1 1 1
3 0 1 1 0
CodePudding user response:
replace
them with 1
.
replace(dat, is.na(dat), 1) |> {\(.) transform(., CVD= (rowSums(.) == 3))}()
# Stroke MI BP CVD
# 1 0 1 1 0
# 2 1 1 1 1
# 3 0 1 1 0
Data:
dat <- structure(list(Stroke = c(0L, 1L, 0L), MI = c(1L, NA, NA), BP = c(1L,
NA, 1L)), class = "data.frame", row.names = c(NA, -3L))