Reputation: 35
I'm working on a dataset in R and want to create a new variable based on the values of variable dx1. Here's my code.
Data1$AMI <- Data1$dx1 %in% c("I21.0", "I21.1", "I21.2", "I21.3",
"I21.4", "I21.9", "I21.A")
My question is how to assign the value of AMI based on a number of variables, say dx1 to dx25? In this dataset, dx1 refers to primary diagnosis, dx2 refers to second diagnosis, and so on. Any of them contain the specific diagnosis code (("I21.0", "I21.1", "I21.2", "I21.3", "I21.4", "I21.9", "I21.A") ) will be assigned a value “1”.
If dx1 %in% c("I21.0", "I21.1", "I21.2") or dx2 %in% c("I21.0", "I21.1", "I21.2") or dx3 %in% c("I21.0", "I21.1", "I21.2”), we want the AMI column show “1”.
Upvotes: 1
Views: 65
Reputation: 26225
I may have misunderstood your question; is this what you want to do?
# Load libraries
library(tidyverse)
# Create fake data
dx <- list()
for (i in 1:25){
dx[[i]] <- c(paste("I",
round(rnorm(n = 50, mean = 21, sd = 5), 1),
sep = ""))
}
name_list <- paste("dx", 1:25, sep = "")
Data1 <- as.data.frame(dx, col.names = name_list)
# Create a variable called "AMI" to count the occurrences of values:
# "I21.0","I21.1","I21.2","I21.3","I21.4","I21.9"
Data2 <- Data1 %>%
mutate(AMI = rowSums(
sapply(select(., starts_with("dx")),
function(x) grepl(pattern = paste(c("I21.0","I21.1","I21.2","I21.3","I21.4","I21.9"),
collapse = "|"), x)
))
)
Data2
Here is how to get the new variable "AMI" to show the value "1" for any row when one or more variables from the list 'dx1:dx25' has a value from the list '"I21.0","I21.1","I21.2","I21.3","I21.4","I21.9"':
Data2 <- Data1 %>%
mutate(AMI = ifelse(rowSums(
sapply(select(., starts_with("dx")),
function(x) grepl(pattern = paste(c("I21.0","I21.1","I21.2","I21.3","I21.4","I21.9"),
collapse = "|"), x)
)) > 0, 1, 0)
)
Data2
Upvotes: 2