mercredi 27 mai 2020

Why does my for loop delete the column instead of edditing it, using R?

I have the following loop:

info$atod <- 0
i=1
info$atod <- for (i in nrow(info)) {
   if(info[i,1] %in% agents$id) {
     1
   }
}

The goal of the loop is to replace the value of info$atod with 1, if the first column of the row contains a value which can be found in agents$id.

In my output the info$atod column is simply nonexistent and I've no idea why.

Is there an easier way to change the value?

Here is the info file:

structure(list(id = c(10, 100, 1000, 1000157, 1000183, 1000184, 
1000252, 1000264, 1000440, 1000533, 1000534, 1000567, 1000568, 
100059, 100060, 100061, 1000670, 1000691, 100072, 100073, 1000803, 
1000887, 1000928, 100098, 100099, 1000999, 1001, 1001004, 1001005, 
1001016, 100102, 100103, 100104, 1001046, 1001090, 1001174, 1001186, 
1001232, 1001239, 100125, 100126, 100127, 100128, 1001313, 1001349, 
1001388, 1001456, 100149, 100150, 1001541), age = c(30L, 3L, 
48L, 52L, 32L, 32L, 52L, 28L, 40L, 36L, 38L, 26L, 31L, 25L, 55L, 
52L, 26L, 70L, 71L, 69L, 23L, 59L, 36L, 31L, 32L, 46L, 46L, 41L, 
55L, 64L, 33L, 42L, 5L, 27L, 50L, 50L, 25L, 25L, 48L, 11L, 40L, 
13L, 42L, 48L, 35L, 27L, 50L, 63L, 65L, 40L), bikeAvailability = c("FOR_SOME", 
"FOR_SOME", "FOR_SOME", "FOR_ALL", "FOR_ALL", "FOR_ALL", "FOR_SOME", 
"FOR_ALL", "FOR_SOME", "FOR_NONE", "FOR_NONE", "FOR_ALL", "FOR_ALL", 
"FOR_ALL", "FOR_ALL", "FOR_ALL", "FOR_NONE", "FOR_ALL", "FOR_SOME", 
"FOR_SOME", "FOR_SOME", "FOR_ALL", "FOR_SOME", "FOR_NONE", "FOR_NONE", 
"FOR_ALL", "FOR_SOME", "FOR_ALL", "FOR_NONE", "FOR_ALL", "FOR_ALL", 
"FOR_ALL", "FOR_ALL", "FOR_ALL", "FOR_ALL", "FOR_ALL", "FOR_NONE", 
"FOR_ALL", "FOR_ALL", "FOR_ALL", "FOR_ALL", "FOR_ALL", "FOR_ALL", 
"FOR_SOME", "FOR_ALL", "FOR_SOME", "FOR_NONE", "FOR_ALL", "FOR_ALL", 
"FOR_SOME"), carAvail = c("never", "never", "never", "always", 
"always", "never", "always", "always", "always", "always", "sometimes", 
"always", "always", "sometimes", "always", "always", "always", 
"always", "sometimes", "always", "always", "always", "always", 
"always", "always", "always", "always", "always", "always", "always", 
"always", "always", "never", "always", "always", "always", "always", 
"always", "always", "never", "always", "never", "always", "always", 
"sometimes", "always", "always", "sometimes", "always", "always"
), employed = c("true", "false", "true", "true", "true", "true", 
"true", "true", "true", "true", "true", "true", "true", "true", 
"true", "true", "true", "true", "false", "false", "true", "false", 
"true", "true", "true", "true", "true", "true", "true", "true", 
"true", "true", "false", "true", "true", "true", "true", "true", 
"true", "false", "true", "false", "true", "true", "true", "true", 
"true", "true", "false", "true"), hasLicense = c("no", "no", 
"yes", "yes", "yes", "yes", "yes", "yes", "yes", "yes", "yes", 
"yes", "yes", "yes", "yes", "yes", "yes", "yes", "yes", "yes", 
"yes", "yes", "yes", "yes", "yes", "yes", "yes", "yes", "yes", 
"yes", "yes", "yes", "no", "yes", "yes", "yes", "yes", "yes", 
"yes", "no", "yes", "no", "yes", "yes", "yes", "yes", "yes", 
"yes", "yes", "yes"), home_x = c(2679482, NA, 2678966, 2695732, 
2696352, 2696352, 2694328, 2697294, 2699069, 2700164, 2700164, 
2697871, 2697871, 2690503, 2690503, 2690503, 2699873, NA, 2689797, 
2689797, 2699259, 2700573, 2693973, 2690685, 2690685, 2698825, 
2678966, 2697642, 2694565, 2699619, 2689409, 2689409, NA, 2699448, 
2696182, 2699603, 2699819, 2693418, 2699191, 2689141, 2689141, 
2689141, 2689141, 2695657, 2695975, 2696420, 2696468, 2690515, 
2690515, 2698872), home_y = c(1237545, NA, 1235785, 1259962, 
1261689, 1261689, 1262417, 1261300, 1262338, 1259968, 1259968, 
1260827, 1260827, 1254816, 1254816, 1254816, 1259875, NA, 1254916, 
1254916, 1262206, 1260359, 1262927, 1254061, 1254061, 1260550, 
1235785, 1260602, 1262912, 1260718, 1255542, 1255542, NA, 1260779, 
1262244, 1259995, 1259831, 1262683, 1260397, 1255220, 1255220, 
1255220, 1255220, 1260799, 1261639, 1260531, 1262128, 1254872, 
1254872, 1264962), isCarPassenger = c("false", "true", "false", 
"false", "false", "true", "false", "false", "false", "false", 
"false", "false", "true", "true", "false", "false", "false", 
"true", "false", "false", "true", "false", "false", "false", 
"false", "false", "false", "false", "false", "false", "true", 
"false", "true", "false", "false", "false", "false", "false", 
"false", "false", "false", "false", "false", "false", "false", 
"false", "false", "false", "false", "false"), isOutside = c("true", 
"false", "true", "true", "true", "true", "true", "true", "true", 
"true", "true", "true", "true", "true", "false", "true", "true", 
"true", "true", "false", "true", "true", "true", "false", "true", 
"true", "true", "true", "true", "true", "false", "true", "false", 
"true", "true", "true", "true", "true", "true", "false", "true", 
"true", "false", "true", "true", "true", "true", "false", "true", 
"true"), mzHeadId = c(374775L, 324961L, 137604L, 275258L, 410086L, 
410086L, 256054L, 444050L, 163782L, 453296L, 453296L, 293687L, 
293687L, 484677L, 484677L, 484677L, 301114L, 210862L, 160519L, 
160519L, 164816L, 402005L, 217146L, 483141L, 483141L, 135331L, 
137604L, 112362L, 482631L, 345109L, 242258L, 242258L, 242258L, 
343217L, 387462L, 249340L, 187016L, 202700L, 455039L, 184033L, 
184033L, 184033L, 184033L, 333797L, 191333L, 282717L, 398417L, 
274594L, 274594L, 176820L), mzPersonId = c(281604L, -1L, 496052L, 
212563L, 130607L, 328565L, 324303L, 212965L, 379135L, 289459L, 
148489L, 304301L, 275262L, 132505L, 143962L, 472786L, 118753L, 
312249L, 427391L, 398024L, 370642L, 157572L, 335200L, 272413L, 
160109L, 348386L, 109718L, 365474L, 115833L, 145063L, 157599L, 
104249L, -1L, 116521L, 481965L, 215263L, 244975L, 356536L, 415926L, 
365484L, 448602L, 107348L, 203282L, 215440L, 179202L, 349503L, 
161305L, 296758L, 115768L, 430587L), ptHasGA = c("true", "true", 
"false", "false", "false", "true", "false", "false", "false", 
"false", "true", "false", "false", "true", "false", "false", 
"false", "false", "true", "true", "false", "false", "false", 
"false", "false", "false", "true", "false", "false", "false", 
"false", "false", "true", "false", "false", "false", "true", 
"false", "false", "false", "false", "false", "false", "false", 
"false", "false", "false", "true", "false", "false"), ptHasHalbtax = c("false", 
"true", "false", "false", "true", "false", "false", "true", "true", 
"true", "false", "false", "false", "false", "true", "true", "true", 
"false", "false", "false", "false", "true", "false", "true", 
"true", "true", "false", "false", "false", "true", "true", "false", 
"true", "false", "true", "false", "false", "true", "true", "false", 
"true", "false", "false", "true", "true", "false", "true", "false", 
"false", "false"), ptHasStrecke = c("false", "true", "false", 
"false", "false", "false", "false", "false", "true", "false", 
"false", "false", "false", "false", "false", "false", "false", 
"false", "false", "false", "false", "false", "false", "false", 
"false", "false", "false", "false", "false", "false", "false", 
"false", "true", "false", "false", "false", "false", "false", 
"true", "false", "false", "false", "false", "false", "false", 
"false", "false", "false", "false", "false"), ptHasVerbund = c("false", 
"true", "false", "true", "false", "false", "false", "false", 
"false", "false", "false", "false", "false", "false", "false", 
"false", "false", "true", "false", "false", "true", "false", 
"false", "false", "false", "false", "false", "false", "false", 
"false", "false", "false", "true", "false", "false", "false", 
"false", "false", "false", "false", "false", "false", "false", 
"false", "true", "false", "false", "false", "false", "false"), 
    sex = c("f", "f", "f", "f", "f", "m", "f", "f", "m", "m", 
    "f", "f", "m", "m", "m", "f", "f", "f", "m", "f", "f", "f", 
    "m", "f", "m", "m", "m", "f", "m", "f", "m", "f", "m", "f", 
    "f", "f", "m", "f", "m", "f", "f", "m", "m", "m", "f", "f", 
    "m", "f", "m", "f"), spRegion = c(1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
    statpopHouseholdId = c(201200010000137, 201200010000049, 
    201200010000745, 201202300043212, 201202300041144, 201202300041144, 
    201202300027378, 201202300003914, 201202300008638, 201202300016150, 
    201202300016150, 201202300038775, 201202300038775, 201200520004578, 
    201200520004578, 201200520004578, 201202300016894, 201202300041579, 
    201200520003321, 201200520003321, 201202300039265, 201202300017598, 
    201202300041352, 201200520000028, 201200520000028, 201202300035149, 
    201200010000745, 201202300032984, 201202300028065, 201202300014660, 
    201200520002069, 201200520002069, 201200520002069, 201202300015154, 
    201202300006093, 201202300016476, 201202300016912, 201202300038520, 
    201202300046704, 201200520002574, 201200520002574, 201200520002574, 
    201200520002574, 201202300020483, 201202300005449, 201202300003039, 
    201202300006442, 201200520000135, 201200520000135, 201202300039406
    ), statpopPersonId = c(201240012081086, 201240013385042, 
    201240009138483, 201240010759877, 201240010762942, 201240014545573, 
    201240013629176, 201240015955140, 201240012957576, 201240010786907, 
    201240014232938, 201240010789848, 201240013347870, 201240011051909, 
    201240014657629, 201240016022367, 201240010796520, 201240010798370, 
    201240011073924, 201240014311763, 201240014177159, 201240013952288, 
    201240010968314, 201240011092250, 201240015776466, 201240014590786, 
    201240010131160, 201240010818041, 201240010818079, 201240010819153, 
    201240011096282, 201240011356900, 201240013062288, 201240011091717, 
    201240014421721, 201240010827760, 201240010828440, 201240010833899, 
    201240014519697, 201240011105988, 201240012332003, 201240014135425, 
    201240014816773, 201240010837196, 201240010841553, 201240013723781, 
    201240011444715, 201240011125001, 201240011217121, 201240012635057
    ), isFreight = c("", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", ""), subpopulation = c("", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", ""), type = c("", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "")), row.names = c(NA, 
50L), class = "data.frame")

and here the agents file:

structure(list(id = c(10, 100061, 100098, 100102, 100126, 100128, 
100149, 100220, 100253, 100271, 100464, 100607, 100648, 100757, 
100761, 100818, 100960, 101, 101111, 101252, 101267, 101274, 
101311, 101387, 101412, 101432, 101433, 101572, 101624, 101627, 
101798, 101799, 101800, 101812, 101847, 101849, 101864, 101915, 
101930, 101993, 102009, 102038, 102039, 102051, 102142, 102143, 
102171, 102172, 102179, 102180, 102190, 102315, 102405, 102415, 
102427, 102529, 102537, 102549, 102624, 102634, 102638, 102661, 
102735, 102783, 102799, 102800, 102801, 102812, 102837, 102850, 
102878, 102879, 102898, 102960, 102969, 102970, 103, 103011, 
103012, 103071, 1040494, 1040529, 1040622, 1040652, 1040668, 
1040681, 1040682, 1040683, 1040733, 1040777, 1040778, 1040779, 
1040781, 1040804, 1040805, 1040806, 1040812, 1040813, 1040814, 
1040815), atod = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1)), row.names = c(NA, 100L), class = "data.frame")

Thank you very much for your help!

Aucun commentaire:

Enregistrer un commentaire