r - Amending a dataset using multiple 'Or' statements to match column data -
i have dataset consisting of hundreds of diagnosis codes. i'm intending reduce these broader condition. example,
dt <- data.frame(diagnosis=c("a415","a419","b519","b589","t814"),broader.condition=na)
is snapshot of current data. thought might able iterate through each diagnosis code, check if 1 interested in, input broader diagnosis corresponding column, here attempt
for(i in 1:length(dt$diagnosis)){ if(dt$diagnosis[i] == "a415"||"a419"||"b519"||"b589"||"t814"){ dt$broader.condition[i] = "skull , face fractures"}
however don't believe i'm using ||
'or' statements correctly throws
"error in dt$diagnosis[i] == "a415" || "a419" : invalid 'y' type in 'x'||'y'
any advice on or 'or' statements within loops appreciated. i'm going extend each code , corresponding broader condition using multiple 'if' statements in 'for' loop.
you have unbalanced brackets, way. removed brackets, useless here.
way 1:
for(i in 1:length(dt$diagnosis)) if(dt$diagnosis[i] == "a415"||dt$diagnosis[i] == "a419"||dt$diagnosis[i] == "b519"||dt$diagnosis[i] == "b589"||dt$diagnosis[i] == "t814") dt$broader.condition[i] = "skull , face fractures"
way 2:
for(i in 1:length(dt$diagnosis)) if(dt$diagnosis[i] %in% c("a415","a419","b519","b589","t814")) dt$broader.condition[i] = "skull , face fractures"
way 3 (note: akrun posted similar solution 4 minutes earlier posted message)
is.skull.fractured = dt$diagnosis %in% c("a415","a419","b519","b589","t814") dt$broader.condition[is.skull.fractured] = "skull , face fractures"
Comments
Post a Comment