create multiple variables with for function

I want to create multiple variables within a dataframe using a loop, its say:
i want reduce the next expression:

lfp1 <- lfp0 %>%
  group_by(id) %>% 
  mutate(kids2 = kids[period==2],
         kids3 = kids[period==3],
         kids4 = kids[period==4],
         kids5 = kids[period==5],
         lhinc2 = lhinc[period==2],
         lhinc3 = lhinc[period==3],
         lhinc4 = lhinc[period==4],
         lhinc5 = lhinc[period==5])

the following stata code does the same

forv i= 2/5 {
by id: gen kids`i' = kids[`i']
}

forv i = 2/5 {
by id: gen lhinc`i' = lhinc[`i']
}

the basis idea is create a variable for the periods 2 to 5 with the obs that correspond to each year for each group.

Can you show us what lfp0 looks like?

1 Like

maybe this?


lfp0 %>% select(id,kids,lhinc,period) %>%
 filter(between(period,2,5)) %>%
mutate(period2=period) %>%
  pivot_wider(names_from=period, values_from=kids, names_prefix="kids") %>%
  pivot_wider(names_from=period2,values_from=lhinc,names_prefix="lhinc")
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

its the data:

structure(list(id = structure(c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 
3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 
7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 
11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 14, 
14, 14, 14, 14, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17, 17, 
17, 17, 17, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 20, 20, 20, 
20, 20), label = "identifier, 1 through 5,663", format.stata = "%9.0g"), 
    period = structure(c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 
    3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 
    2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 
    1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 
    5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 
    4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5), label = "1 through 5, each 4 months long", format.stata = "%9.0g"), 
    lfp = structure(c(0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 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, 0, 
    0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 
    0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), label = "=1 if in labor force", format.stata = "%9.0g"), 
    black = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 
    1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), label = "=1 if black", format.stata = "%9.0g"), 
    educ = structure(c(12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
    15, 15, 15, 15, 15, 12, 12, 12, 12, 12, 14, 14, 14, 14, 14, 
    10, 10, 10, 10, 10, 12, 12, 12, 12, 12, 18, 18, 18, 18, 18, 
    12, 12, 12, 12, 12, 14, 14, 14, 14, 14, 16, 16, 16, 16, 16, 
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 11, 11, 11, 11, 11, 
    7, 7, 7, 7, 7, 16, 16, 16, 16, 16, 12, 12, 12, 12, 12, 15, 
    15, 15, 15, 15, 16, 16, 16, 16, 16, 10, 10, 10, 10, 10), label = "years of schooling, period 1", format.stata = "%9.0g"), 
    age = structure(c(47, 47, 47, 47, 47, 26, 26, 26, 26, 26, 
    34, 34, 34, 34, 34, 52, 52, 52, 52, 52, 44, 44, 44, 44, 44, 
    51, 51, 51, 51, 51, 37, 37, 37, 37, 37, 29, 29, 29, 29, 29, 
    39, 39, 39, 39, 39, 34, 34, 34, 34, 34, 26, 26, 26, 26, 26, 
    31, 31, 31, 31, 31, 41, 41, 41, 41, 41, 37, 37, 37, 37, 37, 
    53, 53, 53, 53, 53, 23, 23, 23, 23, 23, 62, 62, 62, 62, 62, 
    50, 50, 50, 50, 50, 34, 34, 34, 34, 34, 56, 56, 56, 56, 56
    ), label = "in years, period 1", format.stata = "%9.0g"), 
    agesq = structure(c(2209, 2209, 2209, 2209, 2209, 676, 676, 
    676, 676, 676, 1156, 1156, 1156, 1156, 1156, 2704, 2704, 
    2704, 2704, 2704, 1936, 1936, 1936, 1936, 1936, 2601, 2601, 
    2601, 2601, 2601, 1369, 1369, 1369, 1369, 1369, 841, 841, 
    841, 841, 841, 1521, 1521, 1521, 1521, 1521, 1156, 1156, 
    1156, 1156, 1156, 676, 676, 676, 676, 676, 961, 961, 961, 
    961, 961, 1681, 1681, 1681, 1681, 1681, 1369, 1369, 1369, 
    1369, 1369, 2809, 2809, 2809, 2809, 2809, 529, 529, 529, 
    529, 529, 3844, 3844, 3844, 3844, 3844, 2500, 2500, 2500, 
    2500, 2500, 1156, 1156, 1156, 1156, 1156, 3136, 3136, 3136, 
    3136, 3136), label = "age^2", format.stata = "%9.0g"), kids = structure(c(1, 
    1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 
    2, 2, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 
    0, 0, 0, 0), label = "number children < 18", format.stata = "%9.0g"), 
    hinc = structure(c(4933.7412109375, 5582.05712890625, 5723.00537109375, 
    6678.28076171875, 8496.9326171875, 2760.7451171875, 2900.658203125, 
    2826.94897460938, 2868.13891601562, 3040.07104492188, 1021.09350585938, 
    1006.15875244141, 3080.75024414062, 1696.77282714844, 1705.82702636719, 
    1352.88134765625, 1221.33911132812, 1482.58471679688, 1196.79370117188, 
    1226.38830566406, 12919.3837890625, 9125.85546875, 8194.2080078125, 
    10531.388671875, 7963.09130859375, 1206.61755371094, 1568.69799804688, 
    1328.48999023438, 1281.86547851562, 1300.30224609375, 2308.28100585938, 
    2529.0244140625, 2462.3212890625, 2427.5625, 2409.66015625, 
    3603.62768554688, 1509.6044921875, 1489.66760253906, 1413.33898925781, 
    1215.14672851562, 1122.17236328125, 1448.32495117188, 1318.72241210938, 
    1069.80444335938, 1157.05993652344, 2240.5712890625, 2039.40576171875, 
    2068.349609375, 2294.04711914062, 2324.11767578125, 2555.28759765625, 
    2620.03662109375, 2607.146484375, 2634.84521484375, 3159.86547851562, 
    2335.74194335938, 2505.75170898438, 2686.28515625, 3099.04077148438, 
    3358.66528320312, 3778.03198242188, 3975.62475585938, 4096.828125, 
    4188.78125, 4157.091796875, 1751.4716796875, 1846.9462890625, 
    4373.07861328125, 2544.08544921875, 2775.92700195312, 1896.42565917969, 
    2478.16772460938, 2773.09057617188, 1800.76000976562, 1378.35595703125, 
    1492.83972167969, 2287.75390625, 1541.47729492188, 1526.611328125, 
    1602.03698730469, 3566.42651367188, 3758.001953125, 3458.41040039062, 
    3625.76245117188, 3390.99145507812, 1586.85400390625, 1984.33581542969, 
    1841.8154296875, 2070.7060546875, 1975.80895996094, 4605.42822265625, 
    4551.65576171875, 4688.78857421875, 4640.23876953125, 4592.677734375, 
    1620.3154296875, 1832.38269042969, 2879.94384765625, 5241.53271484375, 
    3810.48950195312), label = "husband's monthly income, $", format.stata = "%9.0g"), 
    per1 = structure(c(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 
    0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 
    0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
    1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 
    0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0), label = "=1 if period == 1", format.stata = "%9.0g"), 
    per2 = structure(c(0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 
    0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 
    0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
    1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 
    0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0), label = "=1 if period == 2", format.stata = "%9.0g"), 
    per3 = structure(c(0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 
    0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
    1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 
    0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 
    0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0), label = "=1 if period == 3", format.stata = "%9.0g"), 
    per4 = structure(c(0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
    1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 
    0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 
    0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 
    0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0), label = "=1 if period == 4", format.stata = "%9.0g"), 
    per5 = structure(c(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 
    0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 
    0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 
    0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
    1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), label = "=1 if period == 5", format.stata = "%9.0g"), 
    lhinc = structure(c(8.50385284423828, 8.62731266021729, 8.65224933624268, 
    8.80661582946777, 9.04746055603027, 7.92325592041016, 7.97269296646118, 
    7.94695329666138, 7.96141862869263, 8.0196361541748, 6.92862939834595, 
    6.91389513015747, 8.03292846679688, 7.43648338317871, 7.44180536270142, 
    7.20999193191528, 7.10770320892334, 7.30154228210449, 7.08740139007568, 
    7.11182880401611, 9.46648406982422, 9.11886692047119, 9.01118278503418, 
    9.26211547851562, 8.98257255554199, 7.09557628631592, 7.35800123214722, 
    7.19179821014404, 7.15607166290283, 7.1703519821167, 7.74425840377808, 
    7.83558893203735, 7.80885982513428, 7.79464292526245, 7.78724098205566, 
    8.18969631195068, 7.31960296630859, 7.30630826950073, 7.25371026992798, 
    7.10262012481689, 7.02302169799805, 7.27816295623779, 7.18441867828369, 
    6.9752311706543, 7.05363750457764, 7.71448612213135, 7.6204137802124, 
    7.63450622558594, 7.73807287216187, 7.75109577178955, 7.84592008590698, 
    7.87094354629517, 7.86601161956787, 7.87657976150513, 8.05828475952148, 
    7.75608491897583, 7.82634401321411, 7.89591455459595, 8.03884792327881, 
    8.11929893493652, 8.23695850372314, 8.28793716430664, 8.31796836853027, 
    8.34016513824463, 8.33257102966309, 7.46821165084839, 7.52128887176514, 
    8.38322257995605, 7.8415265083313, 7.92874002456665, 7.54772615432739, 
    7.81527471542358, 7.92771768569946, 7.49596405029297, 7.22864675521851, 
    7.30843544006348, 7.73532581329346, 7.34049654006958, 7.33080577850342, 
    7.37903118133545, 8.17931938171387, 8.2316427230835, 8.14856433868408, 
    8.19581985473633, 8.12887763977051, 7.36950874328613, 7.59303951263428, 
    7.51850700378418, 7.63564491271973, 7.58873319625854, 8.43499088287354, 
    8.42324638366699, 8.45292949676514, 8.44252109527588, 8.43221855163574, 
    7.39037609100342, 7.51337242126465, 7.96552610397339, 8.56436920166016, 
    8.24551296234131), label = "log(hinc)", format.stata = "%9.0g")), row.names = c(NA, 
-100L), class = c("tbl_df", "tbl", "data.frame"))
1 Like