r - 'ignoring' missing data in condition for index -
i trying create index increases 1 if condition fulfilled. code seems work if there no missing data. however, if there missing data, index becomes "na". how can avoid (basically ignoring missing data)?
i tried na.rm=true/false, far can tell refers functions. assume there rather straight forward solution this? many thanks.
here code:
iaep$psindex <- 0 iaep$psindex[iaep$lelecsystem==3] <- 1 iaep$psindex <- iaep$psindex + (iaep$govstruct==3) iaep$psindex <- iaep$psindex + (iaep$reservedseat==2) iaep$psindex <- iaep$psindex + (iaep$uppub==1) iaep$psindex <- iaep$psindex + (iaep$bankpol==1) iaep$psindex <- iaep$psindex + (iaep$execveto==1 & iaep$legveto==1)
here sample data:
iaep <- as.data.frame(structure(list(cowc = structure(c(18l, 18l, 18l, 18l, 18l, 18l, 18l, 18l, 18l, 18l), .label = c("afg", "alb", "alg", "ang", "arg", "arm", "aul", "aus", "aze", "bah", "bel", "ben", "bfo", "bhu", "blr", "bng", "bol", "bos", "bot", "bra", "bui", "bul", "cam", "can", "cao", "cdi", "cen", "cha", "chl", "chn", "col", "com", "con", "cos", "cro", "cub", "cyp", "cze", "czr", "den", "dji", "dom", "drc", "drv", "ecu", "egy", "eqg", "eri", "est", "eth", "etm", "fij", "fin", "frn", "gab", "gam", "gdr", "gfr", "gha", "gmy", "gnb", "grc", "grg", "gua", "gui", "guy", "hai", "hon", "hun", "ind", "ins", "ire", "irn", "irq", "isr", "ita", "jam", "jor", "jpn", "ken", "kuw", "kyr", "kzk", "lao", "lat", "lbr", "leb", "les", "lib", "lit", "maa", "mac", "mag", "mal", "mas", "maw", "mex", "mld", "mli", "mon", "mor", "mya", "mzm", "nam", "nep", "new", "nic", "nig", "nir", "nor", "nth", "oma", "pak", "pan", "par", "per", "phi", "png", "pol", "por", "prk", "qat", "rok", "rom", "rus", "rvn", "rwa", "saf", "sal", "sau", "sen", "sie", "sin", "slo", "slv", "sol", "som", "spn", "sri", "sud", "swa", "swd", "swz", "syr", "taj", "taw", "taz", "thi", "tkm", "tog", "tri", "tun", "tur", "uae", "uga", "ukg", "ukr", "uru", "usa", "uzb", "ven", "yar", "yem", "ypr", "yug", "zam", "zim" ), class = "factor"), year = 1993:2002, psindex = c(na, na, na, 4, 4, 4, 4, 4, 4, 4), lelecsystem = c(3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l), govstruct = c(na, na, na, 3l, 3l, 3l, 3l, 3l, 3l, 3l), courtexec = c(na, na, na, 0l, 0l, 0l, 0l, 0l, 0l, 0l ), reservedseat = c(na, na, na, 2l, 2l, 2l, 2l, 2l, 2l, 2l), uppub = c(1l, 1l, 1l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), bankpol = c(na, na, na, 1l, 1l, 1l, 1l, 1l, 1l, 1l), execveto = c(na, na, na, 0l, 0l, 0l, 0l, 0l, 0l, 0l), legveto = c(na, na, na, 1l, 1l, 1l, 1l, 1l, 1l, 1l)), .names = c("cowc", "year", "psindex", "lelecsystem", "govstruct", "courtexec", "reservedseat", "uppub", "bankpol", "execveto", "legveto"), row.names = 1474:1483, class = "data.frame"))
update/clarification: e.g. in years 1993 - 1995 index na because values in e.g. govstruct missing. instead of na in index have value 2 since 2 conditions index fulfilled (leclecsystem==3 , uppub==1).
cowc year psindex lelecsystem govstruct courtexec reservedseat uppub bankpol execveto legveto 1 bos 1993 na 3 na na na 1 na na na 2 bos 1994 na 3 na na na 1 na na na 3 bos 1995 na 3 na na na 1 na na na 4 bos 1996 4 3 3 0 2 0 1 0 1 5 bos 1997 4 3 3 0 2 0 1 0 1 6 bos 1998 4 3 3 0 2 0 1 0 1 7 bos 1999 4 3 3 0 2 0 1 0 1 8 bos 2000 4 3 3 0 2 0 1 0 1 9 bos 2001 4 3 3 0 2 0 1 0 1 10 bos 2002 4 3 3 0 2 0 1 0 1
here solution:
conds <- cbind(iaep$govstruct==3, iaep$reservedseat==2, iaep$uppub==1, iaep$bankpol==1, iaep$execveto==1 & iaep$legveto==1) iaep$psindex <- apply(conds, 1, sum, na.rm=t)
Comments
Post a Comment