mardi 3 mars 2020

Create new conditional columns with factors using fewer scripts

I would like to know if there is a way to more elegantly rewrite this piece of script. I have tried case_when but it throws an error message when I try to have several of them within one mutate function. Here is the dput for the file

structure(list(todays_date = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 4L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 4L, 4L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 5L, 5L, 5L, 2L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 8L, 9L, 9L, 9L, 9L), .Label = c("04/11/2019", "05/11/2019", "06/11/2019", "07/11/2019", "08/11/2019", "12/11/2019", "13/11/2019", "14/11/2019", "15/11/2019"), class = "factor"), p_initials = structure(c(34L, 54L, 1L, 71L, 16L, 77L, NA, 55L, 56L, 122L, 20L, 53L, 116L, 48L, 36L, 14L, 44L, 55L, 89L, 96L, 105L, 83L, 92L, 98L, 38L, 5L, 70L, 47L, 10L, 10L, 107L, 67L, 70L, 24L, 25L, 32L, 65L, 24L, 124L, 87L, 75L, 80L, 26L, 31L, 112L, 40L, 45L, 117L, 10L, 23L, 11L, 69L, 7L, 8L, 6L, 79L, 81L, 46L, 108L, 13L, 3L, 61L, 82L, 65L, 90L, 102L, 101L, 59L, 93L, 70L, 74L, 29L, 62L, 78L, 67L, 13L, 64L, 119L, 22L, 43L, 10L, 38L, 50L, 104L, 3L, 2L, 125L, 13L, 88L, 4L, 96L, 106L, 84L, 109L, 17L, 74L, 10L, 91L, 63L, 89L, 7L, 120L, 12L, 38L, 95L, 27L, 9L, 86L, 42L, 99L, 70L, 110L, 103L, 74L, 111L, 72L, 85L, 68L, 76L, 73L, 70L, 21L, 77L, 37L, 8L, 66L, 70L, 123L, 94L, 61L, 115L, 25L, 120L, 67L, 119L, 19L, 71L, 21L, 34L, 57L, 42L, 57L, 100L, 18L, 30L, 19L, 105L, 113L, 39L, 60L, 15L, 33L, 95L, 121L, 52L, 97L, 102L, 5L, 58L, 81L, 114L, 119L, 28L, 3L, 7L, 51L, 35L), .Label = c("BA", "BB", "BD", "BE", "BH", "BI", "BM", "BS", "BY", "CA", "CB", "CD", "CE", "CF", "CG", "CGA", "CGG", "CI", "CK", "CL", "CM", "CO", "CP", "CS", "CT", "CZ", "DK", "DO", "DPH", "DT", "GA", "GB", "GG", "IA", "IB", "Ik", "IK", "IM", "IP", "IS", "ITF", "KA", "KB", "KBA", "KF", "KG", "KJ", "KK", "KM", "KO", "KP", "KR", "KS", "KY", "NB", "ND", "NF", "NG", "NI", "NJ", "NK", "NKD", "NL", "NM", "NR", "NRBS", "NT", "NWD", "NY", "OA", "OB", "OC", "OD", "OH", "OHD", "OI", "OJ", "OK", "OL", "OM", "OP", "OPI", "OS", "OSP", "OT", "OTL", "PR", "PS", "SA", "SG", "SH", "SJ", "SLP", "SM", "SP", "SS", "TA", "TBC", "TE", "TG", "TKP", "TM", "TMB", "TP", "TR", "TS", "WJ", "WR", "YH", "YKI", "YM", "ZA", "ZB", "ZE", "ZH", "ZK", "ZM", "ZN", "ZP", "ZS", "ZSS", "ZT", "ZTM", "ZTN", "ZZ"), class = "factor"), village = structure(c(2L, 2L, 2L, 2L, 3L, 3L, 8L, 1L, 1L, 1L, 8L, 8L, 8L, 8L, 6L, 6L, 8L, 8L, 8L, 8L, 8L, 1L, 1L, 1L, 8L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 1L, 8L, 8L, 5L, 5L, 5L, 3L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 2L, 2L, 7L, 7L, 7L, 4L, 4L, 4L, 7L, 7L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 7L, 7L, 7L, 8L, 8L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 1L, 4L, 4L, 4L, 4L, 3L, 6L, 6L, 8L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 8L, 8L, 8L, 8L, 8L, 1L, 1L, 4L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 7L, 7L), .Label = c("banembanto", "bankore", "damzoussi", "pissy", "sabsin", "tanghin", "toundou", "watenga"), class = "factor"), compound_id = c("40080", "40093", "40113", "040127", "240043", "240060", "250035", "230047", "230033", "230049", "250014", "250031", "250002", "250051", "220040", "220080", "250056", "250045", "250061", "250042", "250811", "230068", "230104", "230144", "250062", "40144", "40814", "030015", "030022", "030108", "30156", "30001", "30002", "30052", "30089", "30069", "30083", "030094", "30144", "30161", "30192", "30004", "030006", "030025", "30055", "30202", "30205", "30239", "30259", "30809", "40053", "40086", "40109", "040116", "40823", "30197", "30216", "30237", "30159", "30167", "30219", "30223", "260041", "260803", "260055", "260015", "230098", "230102", "230111", "230145", "250805", "250810", "260004", "260023", "260032", "240065", "260025", "260075", "260049", "30012", "030023", "030030", "30057", "40055", "40118", "80044", "80068", "80075", "30203", "30229", "30238", "80001", "80007", "220041", "220042", "220022", "220083", "230115", "230048", "230097", "230072", "80055", "80803", "80807", "250809", "250806", "220034", "220019", "220064", "220840", "220001", "220118", "220175", "220834", "220070", "220099", "220098", "220141", "220805", "220849", "230174", "030110", "30146", "30190", "30215", "240006", "220097", "220823", "250016", "240010", "240042", "240049", "240080", "240073", "240067", "30265", "30822", "30823", "240004", "230040", "230057", "230078", "230158", "240021", "240053", "240054", "240064", "240066", "240086", "250009", "250028", "250039", "250053", "250063", "230150", "230164", "30828", "40094", "240007", "240013", "240071", "240078", "040018", "040125", "40147", "80034", "80049"), new_compound_id = c(40080L, NA, NA, NA, NA, NA, NA, NA, 230033L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 30156L, NA, NA, 30052L, NA, NA, NA, NA, NA, NA, 30192L, NA, NA, NA, NA, 30202L, NA, NA, NA, NA, 40053L, NA, NA, NA, NA, 30197L, 30216L, 30237L, NA, NA, 30219L, 30223L, NA, NA, 260055L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 260075L, 260049L, NA, NA, NA, NA, NA, NA, NA, 80068L, NA, 30203L, 30229L, NA, NA, 80007L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 220840L, NA, NA, NA, NA, NA, NA, NA, NA, 220805L, NA, NA, NA, NA, 30190L, NA, NA, NA, NA, 250016L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 30828L, 40094L, NA, NA, NA, NA, NA, NA, NA, NA, NA), num_sleep_space = c(2L, 3L, 2L, 2L, 3L, 4L, 2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, 14L, 11L, 14L, 12L, 12L, 14L, 14L, 12L, 14L, 14L, 11L, 12L, 21L, 14L, 21L, 11L, 21L, 14L, 11L, 11L, 15L, 2L, 2L, 8L, 10L, 9L, 9L, 22L, 21L, 23L, 23L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 7L, 6L, 21L, 2L, 2L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 12L, 14L, 14L, 12L, 15L, 17L, 11L, 16L, 14L, 14L, 11L, 14L, 21L, 2L, 2L, 2L, 2L, 2L, 4L, 21L, 9L, 9L, 23L, 23L, 23L, 23L, 23L, 14L, 1L, 14L, 14L, 14L, 13L, 14L, 14L, 4L, 4L, 4L, 21L, 21L, 21L, 21L, 21L, 9L, 21L, 21L, 21L, 21L, 21L, 21L, 23L, 23L, 23L, 23L, 23L, 4L, 4L, 4L, 4L, 14L, 12L, 16L, 18L, 14L, 14L, 14L, 23L, 23L, 14L, 4L, 4L, 2L, 14L, 12L, 14L, 14L, 14L, 16L, 12L, 12L, 14L, 12L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 18L, 4L, 2L, 19L, 19L, 16L, 20L, 2L, 3L, 5L, 2L, 2L), .Label = c("12/07/2019", "15/06/2019", "15/07/2019", "16/06/2019", "16/07/2019", "17/06/2019", "17/10/2019", "18/06/2019", "19/06/2019", "20/06/2019", "20/07/2019", "21/07/2019", "22/06/2019", "22/07/2019", "23/06/2019", "23/07/2019", "24/06/2019", "24/07/2019", "25/06/2019", "25/07/2019", "29/06/2019", "29/10/2019", "30/06/2019" ), class = "factor"), note_net_type.brand_net_given = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 9L, 9L, 9L, 9L, 9L, 2L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 1L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 3L, 5L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 1L, 6L, 9L, 9L, 6L, 12L, 1L, 11L, 12L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 8L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 4L, 7L, 3L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 4L, 7L, 6L, 12L, 13L, 12L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 12L, 12L, 7L, 7L, 1L, 12L, 12L, 12L, 10L, 7L, 5L, 7L, 7L), .Label = c("", "Pema.net", "PERMA .NET", "PERMA,NET", "PERMA. NET", "Perma.net", "PERMA.NET", "Perman.net", "Permanet", "PERMANET", "Permanet.2", "PERMANET.2", "PERMANT.2" ), class = "factor"), note_net_type.help_hang_net = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L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A, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), num_old_net = c(2L, 3L, 2L, 2L, 4L, 6L, 3L, 3L, 4L, 2L, 4L, 5L, 1L, 3L, 6L, 4L, 3L, 2L, 4L, 4L, 3L, 1L, 4L, 4L, 3L, 0L, 2L, 0L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 5L, 4L, 3L, 6L, 6L, 4L, 5L, 6L, 4L, 6L, 5L, 6L, 6L, 5L, 4L, 4L, 4L, 3L, 6L, 6L, 3L, 3L, 3L, 2L, 1L, 3L, 3L, 4L, 3L, 4L, 4L, 3L, 4L, 6L, 5L, 1L, 3L, 4L, 5L, 4L, 5L, 0L, 0L, 2L, 4L, 3L, 4L, 4L, 5L, 4L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 2L, 1L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 5L, 4L, 5L, 3L, 3L, 7L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 3L, 4L, 2L, 3L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 3L, 2L, 4L, 2L, 2L, 5L,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 5L, 5L, 8L, 6L, 5L, 5L, 6L, 7L, 2L, 5L, 5L, 7L, 5L, 8L, 6L, 8L, 5L, 8L, 7L, 6L, 6L, 7L, 10L, 8L, 10L, 5L, 5L, 6L, 5L, 4L, 5L, 5L, 6L, 6L, 4L, 4L, 3L, 5L, 7L, 8L, 7L, 5L, 10L, 10L, 6L, 2L, 4L, 6L, 4L, 10L, 5L, 5L, 5L, 5L, 6L, 12L, 5L, 5L, 4L, 7L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 6L, 5L, 9L, 5L, 5L, 5L, 6L, 9L, 9L, 6L, 10L, 6L, 5L, 5L, 11L, 10L, 3L, 6L, 5L, 5L, 11L, 8L, 5L, 9L, 10L, 18L, 12L, 12L, 19L, 6L, 15L, 10L, 9L, 7L, 10L, 8L, 22L, 30L, 5L, 6L, 19L, 11L, 5L, 15L, 7L, 7L, 6L, 5L, 6L)), class = "data.frame", row.names = c(NA, -167L))

and the script I want to rewrite

comp_df <- comp_df %>%mutate(`sleep space category` = ifelse(num_sleep_space == 1, "1", ifelse(num_sleep_space >=2
                                            & num_sleep_space <=4 ,"2-4",ifelse(num_sleep_space >=5 & num_sleep_space <=9,
                                            "5-9", ifelse(num_sleep_space >9, ">9", NA)))),
        `sleep space category`  = factor(`sleep space category` , levels=c("1","2-4","5-9",">9")), 
                  `number of nets given` = ifelse(note_net_type.num_net_given == 1, "1", 
                  ifelse(note_net_type.num_net_given >=2 & note_net_type.num_net_given <=4 ,"2-4",
                  ifelse(note_net_type.num_net_given >=5 & note_net_type.num_net_given <=9,"5-9",
                         ifelse(note_net_type.num_net_given >9, ">9", NA)))),  
        `number of nets given` = factor(`number of nets given`, levels = c("1","2-4","5-9",">9")), 
        `net surplus/gap` = num_sleep_space - note_net_type.num_net_given, 
        `number of household members` = ifelse(hh_member_count >= 1 & hh_member_count<= 5, "1-5",
                  ifelse(hh_member_count >=6 & hh_member_count <=10,"6-10",ifelse(hh_member_count >10, ">10", NA)))) %>% 
                  mutate(`number of household members` = factor(`number of household members`,
                          levels = c("1-5","6-10",">10")))

Aucun commentaire:

Enregistrer un commentaire