Не разделяя мои данные, как я могу создать новую категориальную переменную с помощью функции в R - PullRequest
1 голос
/ 07 мая 2020

Я хотел бы создать новую категориальную переменную в моем вызове набора данных Age_bucket, который основан на Paid_age. Но состояние Age_bucket отличается для двух компаний. Итак, могу ли я создать функцию Age_bucket для обеих компаний, а скорее разделить мои данные, а затем присоединиться к ним, что не является разумным подходом. я привел ниже код age_bucket для двух компаний.

 if(unique(mydata$Company)=='2002'){
    mydata$Age_Bucket = ifelse((mydata$Paid_age <= -8), 'a_less_than-8',
                                       ifelse((mydata$Paid_age >-8 & mydata$Paid_age <=0 ), 'b_Between -8 to 0',
                                              ifelse((mydata$Paid_age >0 & mydata$Paid_age <=7 ), 'c_0 to 7',
                                                     ifelse((mydata$Paid_age >7 & mydata$Paid_age <=25 ), 'd_7 to 25',
                                                            'e_Greater than 25'))))
    if(unique(mydata$Company)=='2203'){
    mydata$Age_Bucket = ifelse((mydata$Paid_age <= -9), 'a_less_than-9',
                                        ifelse((mydata$Paid_age >-9 & mydata$Paid_age <=-5 ), 'b_Between -9 to -5',
                                               ifelse((mydata$Paid_age >-5 & mydata$Paid_age <=1 ), 'c_-5 to 1',
                                                      ifelse((mydata$Paid_age >1 & mydata$Paid_age <=25 ), 'd_1 to 25',
                                                             'e_Greater than 25'))))
The dataset to be used:
> dput(mydata)
structure(list(Company = c("2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2002", "2002", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203", "2203", "2203", "2203", "2203", 
"2203", "2203", "2203", "2203"), Paid_age = structure(c(6, 365, 
-2, 3, 25, -13, 174, 46, -5, -4, 5, -2, 6, 9, 55, -5, 44, 49, 
120, 31, 28, 0, 6, -3, 0, 132, 0, 37, 31, 111, -1, 211, -11, 
0, 0, 0, 33, 31, -4, -1, -1, 2, 1, -2, 18, 0, 11, 52, 2, 1, -7, 
14, 12, -4, -1, -41, 23, -53, 12, 28, 17, 1, 17, -47, 7, 0, -5, 
0, -6, 0, 26, -4, 8, 11, -1, -3, 8, 64, 22, 29, 103, 21, 29, 
28, -83, 45, -60, 22, 13, 94, 6, -81, -18, -6, -13, 107, -65, 
-71, -11, 2, -6, 70, 10, 0, 18, 4, 26, 4, 0, 3, -7, 57, 20, 217, 
9, 16, 100, -1, -42, -77, 0, 15, 4, -65, 1, 161, 28, 21, -2, 
-21, -80, 26, 0, 0, 0, 18, -42, 0, -18, -30, 99, 375, 103, -83, 
22, -19, 0, -1, 76, 30, 20, 22, -6, 22, 13, 13, 28, 27, -7, -3, 
31, -4, -6, 55, 13, 0, 1, 0, 0, 1, -4, 104, -6, 14, -4, 15, -11, 
19, 27, 23, 76, 38, -77, -4, -27, 20, 80, 22, 31, -5, -8, 3, 
6, -1, 16, -55, 77, 0, 43, 6, 0, 43, 99, 34, 13, 0, 74, 1, 5, 
-5, 0, 61, 150, -22, -6, -4, 21, 24, 9, 0, 57, 43, 10, 3, -5, 
-22, 40, 35, 46, 10, 0, -4, 0, -3, 10, 51, -4, 19, 1, 524, 16, 
2, 0, -24, 20, 140, 0, 5, 19, -8, -11, -2, 39, 19, 25, 19, -1, 
1, -13, 36, -5, 3, -5, 0, -14, 72, 33, 0, 14, 49, 28, 0, 4, 71, 
8, 77, 32, 11, 0, 0, -11, -2, 424, 22, 1, 14, 16, 6, -1, 30, 
25, 13, 29, 0, 1, 49, 4, 955, 240, 3, 2, 5, 3, 5, 28, 27, 173, 
24, 21, 19, 19, 13, 1, 17, -3, 14, 6, 12, 12, -2, 10, 4, 16, 
18, 0, -2, 1, 144, 5, 4, 3, -1, 212, 0, 43, -2, 231, 13, 9, 5, 
77, -2, 24, 442, 381, 4, 18, -4, 40, -3, -5, 7, 18, 17, 14, 111, 
1, 3, 21, 112, 2, 3, -1, 11, -5, -4, -5, 25, -2, -1, 5, 17, 5, 
9, 0, 0, 1, 2, 22, 58, 19, 19, 18, 34, 1, 1, -3, 4, -2, 1, 3, 
27, 40, 15, 55, 18, 1, 21, 33, 20, 19, 15, 10, 94, -3, 92, 0, 
9, -4, -5, 48, 18, 16, 5, 2, 36, 0, 32, -1, 97, -2, 4, 3, -8, 
3, 11, 0, -2, 8, 74, 7, 7, 5, 5, 68, -1, 77, 182, 57, 79, 0, 
9, 20, 0, 6, 30, -2, 2, 40, 8, -4, 18, 0, 158, -4, 6, 25, -3, 
-4, 168, 35, 147, -11, -2, 9, -1, 3, 18, 17, 14, 13, -1, 0, 0, 
-6, -30, -2, -2, 1, 4, -9, 158, 14, -6, 6, 9, 1, 12, -2, 14, 
45, 6, 30, 410, -6, 28, 19, 185, 0, -5, -5, -2, 1, 0, -1, 21, 
19, 12, 7, 18, 8, -5, -2, 11, 7, 18, 20, 5, 2, -4, 31, 1, -2, 
-4, -4, 6, 15, -3, 19, 5, -1, 15, 9, 7, 9, -3, -1, 9, 8, 2, 8, 
112, 17, 19, 0, 268, 6, 5, -54, 11, 33, -2, -5, 39, 18, 15, -2, 
-7, -2, 14, 186, 2, 3, 6, 6, -5, 16, 11, 73, 4, 9, -1, 23, -3, 
-3, 4, -7, -46, -4, -2, 3, 3, 7, 15, 26, 25, 25, -6, 4, -4, 5, 
20, -20, -4, 19, 22, -2, 0, -3, 28, 15, 2, 20, 3, 41, 7, -1, 
16, 13, -7, 16, -6, 2, -3, -1, -2, -5, -5, 86, 7, 9, -7, 3, -3, 
2, -11, 1, -3, -16, 8, -6, 2, 2, 114, 5, 7, -3, -4, 2, 6, 15, 
1, 5, 11, 6, 18, 24, -3, -1, -3, -7, -30, -4, 8, 1, 1, -5, 4, 
0, 15, 39, 21, 16, 36, -1, -4, -7, -1, -5, 32, 31, 30, -7, 0, 
26, 19, 31, 15, -2, 9, -2, 18, 79, 17, 17, 13, 14, -4, 13, 0, 
-1, 13, -3, 2, -12, -6, 6, 11, -2, 65, -1, -1, -3, 26, 0, 16, 
3, 12, -6, -2, 45, 1, 1, 13, 2, -1, 9, 15, 38, 38, 7, 36, -35, 
31, 24, -1, 23, -4, 25, -5, 2, 8, -3, 0, -2, 8, 8, 6, 31, -4, 
-3, -7, 3, 24, 1, 5, 2, 3, 26, -5, -1, 15, 21, 57, -2, 8, -2, 
-3, -7, 5, 6, 62, 4, -7, -7, 1, -2, 16, 5, -3, 15, 1, -2, 10, 
17, -2, 13, -3, 11, -1, -5, 1, 0, 3, -2, -2, -2, -2, -4, 16, 
-5, 27, -3, -4, 26, 16, -10, 25, 2, 24, 6, -2, 10, -3, -1, -1, 
21, 21, -4, 6, 4, 0, 39, 40, 11, -24, -3, 0, 12, 2, -5, 2, 3, 
4, 16, 7, 1, 7, -2, 6, 4, 10, 4, -1, -2, -2, 7, -3, 3, 3, -6, 
8, 9, 2, 17, 32, 35, 29, -4, -1, -3, -1, 10, 1, 0, 14, -2, 1, 
0, -7, -2, -2, 6, 34, -3), class = "difftime", units = "days")), row.names = c("355", 
"952", "1024", "1083", "1222", "1306", "1403", "1421", "1769", 
"1862", "1945", "2046", "2337", "2380", "2410", "2811", "2920", 
"2978", "3193", "3292", "3298", "3441", "3516", "3572", "3712", 
"3939", "4245", "4309", "4310", "4460", "4481", "5080", "5106", 
"5175", "5278", "5340", "5640", "5643", "5693", "6580", "6859", 
"6881", "6962", "6987", "7004", "7038", "7134", "8439", "8571", 
"8662", "8914", "9092", "9094", "9266", "9576", "9788", "9956", 
"9985", "10013", "10108", "10182", "10239", "10246", "10450", 
"10729", "11031", "11276", "12189", "12304", "12371", "12594", 
"13755", "13809", "14012", "14235", "14297", "14406", "14983", 
"15087", "15634", "16227", "16455", "16702", "16861", "17716", 
"17772", "18512", "18729", "18880", "18941", "19328", "19698", 
"19739", "19841", "20701", "20770", "20929", "21064", "21243", 
"21918", "22393", "22826", "22911", "22982", "23036", "23138", 
"23192", "23271", "23314", "23390", "23445", "23591", "23681", 
"23774", "23838", "24002", "24051", "24171", "24206", "24724", 
"24910", "25507", "26050", "26067", "26502", "26527", "26845", 
"27273", "27452", "27613", "27649", "27815", "27897", "28060", 
"28130", "28240", "28949", "29166", "29348", "29392", "29682", 
"30075", "30096", "30406", "30999", "31069", "31199", "31228", 
"31708", "31723", "32519", "32636", "32741", "32840", "33199", 
"33200", "33205", "33250", "33269", "33456", "34198", "35099", 
"35135", "35274", "35284", "35322", "35328", "35600", "35622", 
"36309", "36315", "36766", "36871", "36908", "36976", "36995", 
"37137", "37409", "37460", "37463", "37536", "37631", "37703", 
"37735", "37937", "37953", "38105", "38250", "39470", "39568", 
"39596", "39849", "39894", "40362", "40965", "40981", "41127", 
"41335", "41518", "41615", "41744", "41828", "41896", "41946", 
"42193", "42375", "42467", "42735", "42736", "43072", "43198", 
"43330", "43352", "43519", "43736", "43772", "43787", "43992", 
"44180", "44206", "44380", "44410", "44501", "44528", "44622", 
"44858", "44890", "44904", "44917", "45387", "45611", "45688", 
"45781", "45851", "46197", "46440", "46772", "46929", "47534", 
"47834", "47884", "48057", "48255", "48369", "48384", "48580", 
"48922", "48982", "49268", "49326", "49565", "49587", "49894", 
"50019", "50109", "50327", "50370", "50745", "51084", "51321", 
"51481", "52111", "52174", "52233", "52387", "52864", "52971", 
"53057", "53081", "53559", "54373", "54464", "54816", "54983", 
"55009", "55061", "55161", "55469", "55528", "55549", "55587", 
"56003", "56097", "56445", "56666", "56748", "57195", "57543", 
"57631", "57685", "58128", "58481", "58698", "58752", "58920", 
"59220", "59972", "12", "28100", "52", "105", "118", "13510", 
"139", "184", "194", "196", "243", "254", "276", "277", "297", 
"301", "306", "322", "335", "391", "393", "39610", "431", "444", 
"452", "463", "465", "475", "557", "564", "568", "573", "651", 
"655", "667", "687", "700", "706", "71310", "718", "722", "784", 
"820", "837", "851", "861", "86810", "870", "927", "967", "983", 
"989", "1002", "1007", "104510", "1047", "1051", "1059", "1087", 
"1088", "1103", "1122", "1155", "1156", "1220", "1270", "1286", 
"1397", "1433", "1450", "1476", "1486", "1503", "1519", "1528", 
"1550", "1562", "1579", "1585", "1587", "1599", "1608", "1624", 
"1656", "16582", "1669", "1692", "1720", "1722", "1773", "1776", 
"1805", "1809", "1813", "1818", "1870", "1883", "1903", "1972", 
"19851", "2005", "2028", "2052", "2094", "2129", "2135", "2156", 
"2202", "2222", "2229", "22361", "2244", "2246", "2247", "2290", 
"2294", "23371", "23381", "2340", "2352", "2384", "2390", "2465", 
"2491", "25301", "2565", "2592", "2683", "2688", "2691", "2757", 
"2772", "27821", "2789", "2798", "2826", "2832", "2839", "2844", 
"2847", "28741", "2886", "2927", "2935", "2973", "3027", "3037", 
"3046", "3102", "3115", "3134", "3170", "3205", "3262", "3287", 
"3330", "3341", "3427", "3489", "3533", "3540", "3551", "3569", 
"3597", "3610", "3623", "3631", "3647", "36631", "3676", "3685", 
"3705", "3711", "3755", "3838", "38431", "3967", "3985", "3986", 
"4021", "4025", "4040", "4098", "4144", "4204", "42081", "4210", 
"4222", "42391", "4255", "4258", "4288", "4303", "4318", "4331", 
"4363", "4401", "4437", "4483", "4524", "4567", "4577", "4597", 
"4598", "4611", "4616", "4617", "4630", "4650", "4656", "4681", 
"4686", "46901", "4699", "4741", "4749", "4778", "4821", "4823", 
"4833", "4862", "4918", "4931", "4946", "4947", "5013", "5014", 
"5043", "5052", "5058", "5079", "50981", "5099", "5102", "51061", 
"5107", "5116", "5124", "5145", "51482", "51812", "5232", "5239", 
"5244", "5252", "5255", "5261", "52661", "5271", "5297", "5303", 
"5312", "5328", "5398", "54041", "5423", "5438", "5502", "5515", 
"5546", "5549", "5557", "5564", "5574", "5594", "5599", "5678", 
"5680", "5696", "5697", "5703", "5714", "5723", "5751", "5813", 
"58381", "58441", "5845", "5869", "5897", "5906", "5923", "60311", 
"6038", "6039", "6052", "6054", "6113", "6131", "6135", "6188", 
"6190", "61921", "6206", "6214", "62201", "6228", "6235", "6250", 
"6254", "6261", "6278", "6291", "6299", "6318", "6368", "6405", 
"6491", "6510", "65431", "65491", "6597", "6615", "6621", "6641", 
"6720", "6735", "6748", "6752", "6757", "6759", "67851", "6795", 
"67961", "6848", "6880", "6883", "6903", "6905", "6925", "6937", 
"69811", "7033", "7048", "7060", "7064", "7139", "7157", "7181", 
"7193", "71991", "7204", "7236", "7258", "72591", "7266", "7276", 
"7284", "7300", "7301", "7302", "7304", "7334", "7407", "74291", 
"7483", "7494", "7536", "7538", "7559", "7561", "7563", "7585", 
"7600", "7634", "7650", "7681", "7689", "76911", "7707", "7723", 
"7766", "7767", "7768", "7783", "7837", "7850", "7851", "79511", 
"7992", "8046", "8055", "8080", "8142", "8149", "8159", "8192", 
"8197", "8220", "8232", "8256", "8276", "83161", "8327", "8344", 
"8349", "8351", "8392", "8410", "8427", "8452", "8456", "8483", 
"8487", "8508", "8531", "8575", "8581", "8584", "8598", "8602", 
"8608", "8654", "8681", "8746", "8791", "8807", "8814", "8838", 
"8840", "8867", "8897", "8902", "89351", "8962", "8988", "9013", 
"9106", "9150", "9174", "9212", "9233", "9242", "9254", "9306", 
"9325", "9351", "9386", "9427", "9434", "94561", "9457", "9462", 
"94821", "9543", "96021", "9606", "9631", "9637", "9679", "9686", 
"9691", "9695", "9749", "9750", "9756", "9800", "98171", "9853", 
"9863", "9892", "9912", "9925", "9930", "99461", "9952", "9974", 
"9990", "10008", "10018", "10031", "10034", "10051", "10062", 
"10089", "10148", "101891", "10190", "10222", "102361", "10248", 
"10296", "10317", "10329", "10353", "10360", "10387", "10429", 
"10442", "10455", "104631", "10470", "10476", "10487", "105461", 
"10552", "105611", "10585", "10602", "10620", "10632", "10640", 
"10645", "10648", "10688", "10693", "10763", "10797", "10807", 
"10856", "10862", "10893", "10896", "10933", "10970", "10971", 
"10972", "10976", "10980", "11020", "11035", "11042", "11100", 
"11102", "11156", "11168", "112111", "112341", "11239", "11255", 
"11346", "11349", "11352", "113881", "11393", "11399", "11423", 
"11429", "11441", "11453", "11454", "11464", "11468", "11483", 
"11500", "115251", "11555", "11592", "11595", "11624", "11627", 
"11637", "11674", "11681", "11691", "11708", "11715", "11779", 
"11781", "11793", "11813", "11844", "11893", "11954", "11955", 
"11975", "12044", "12073"), class = "data.frame")

Ответы [ 2 ]

1 голос
/ 07 мая 2020

Я бы использовал within и условное подмножество, этот метод обеспечивает большую ясность.

mydata <- within(mydata, {
  Age_Bucket <- NA  ## initialize new variable
  # 2002
  Age_Bucket[Company == "2002" & Paid_age <= -8] <- 'a_less_than-8'
  Age_Bucket[Company == "2002" & Paid_age > -8 & Paid_age <= 0] <- 'b_Between -8 to 0'
  Age_Bucket[Company == "2002" & Paid_age > 0 & Paid_age <= 7] <- 'c_0 to 7'
  Age_Bucket[Company == "2002" & Paid_age > 7 & Paid_age <= 25] <- 'd_7 to 25'
  Age_Bucket[Company == "2002" & Paid_age > 25] <- 'e_Greater than 25'
  # 2203
  Age_Bucket[Company == "2203" & Paid_age <= -9] <- 'a_less_than-9'
  Age_Bucket[Company == "2203" & Paid_age > -9 & Paid_age <= -5] <- 'b_Between -9 to 5'
  Age_Bucket[Company == "2203" & Paid_age > -5 & Paid_age <= 1] <- 'c_-5 to 1'
  Age_Bucket[Company == "2203" & Paid_age > 1 & Paid_age <= 25] <- 'd_1 to 25'
  Age_Bucket[Company == "2203" & Paid_age > 25] <- 'e_Greater than 25'
})

Результат 1:

mydata[294:301, ]
#       Company Paid_age        Age_Bucket
# 58752    2002   0 days b_Between -8 to 0
# 58920    2002   1 days          c_0 to 7
# 59220    2002  49 days e_Greater than 25
# 59972    2002   4 days          c_0 to 7
# 12       2203 955 days e_Greater than 25
# 28100    2203 240 days e_Greater than 25
# 52       2203   3 days         d_1 to 25
# 105      2203   2 days         d_1 to 25

Возможно, вам также понадобится чтобы изучить функцию cut:

mydata <- within(mydata, {
  Age_Bucket2 <- NA
  Age_Bucket2[Company == "2002"] <- 
    as.character(
      cut(as.numeric(Paid_age[Company == "2002"]), breaks=c(-Inf, -8, 0, 7, 25, Inf), 
        labels=c('a_less_than-8', 'b_Between -8 to 0', 'c_0 to 7', 'd_7 to 25', 
                 'e_Greater than 25')))
  Age_Bucket2[Company == "2203"] <- 
    as.character(
      cut(as.numeric(Paid_age[Company == 2203]), breaks=c(-Inf, -9, -5, 1, 25, Inf), 
          labels=c('a_less_than-9', 'b_Between -9 to 5', 'c_-5 to 1', 'd_1 to 25', 
                   'e_Greater than 25')))
})

Результат 2:

mydata[294:301, ]
#       Company Paid_age        Age_Bucket       Age_Bucket2
# 58752    2002   0 days b_Between -8 to 0 b_Between -8 to 0
# 58920    2002   1 days          c_0 to 7          c_0 to 7
# 59220    2002  49 days e_Greater than 25 e_Greater than 25
# 59972    2002   4 days          c_0 to 7          c_0 to 7
# 12       2203 955 days e_Greater than 25 e_Greater than 25
# 28100    2203 240 days e_Greater than 25 e_Greater than 25
# 52       2203   3 days         d_1 to 25         d_1 to 25
# 105      2203   2 days         d_1 to 25         d_1 to 25
0 голосов
/ 07 мая 2020

Используйте case_when из dplyr:

library(dplyr)

mydata %>%
  mutate(Paid_age = as.numeric(Paid_age),
         Age_Bucket = case_when(
    Company == 2002 & Paid_age <= -8 ~ 'a_less_than-8', 
    Company == 2002 & Paid_age > -8 & Paid_age <= 0 ~ 'b_Between -8 to 0',
    Company == 2002 & Paid_age > 0 & Paid_age <= 7 ~ 'c_0 to 7',
    Company == 2002 & Paid_age > 7 & Paid_age <= 25 ~ 'd_7 to 25',
    Company == 2002 & Paid_age > 25  ~ 'e_Greater than 25',
    Company == 2203 & Paid_age <= -9  ~ 'a_less_than-9',
    Company == 2203 & Paid_age > -9 & Paid_age <= -5 ~ 'b_Between -9 to -5',
    Company == 2203 & Paid_age > -5 & Paid_age <= 1 ~ 'c_-5 to 1',
    Company == 2203 & Paid_age > 1 & Paid_age <= 25 ~ 'd_1 to 25',
    Company == 2203 & Paid_age > 25  ~ 'e_Greater than 25'))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...