将数据转换为选择模型数据结构

时间:2019-01-04 21:28:27

标签: r

我在L5num中列出了10种产品,并且我有PANID。我正在尝试为多项逻辑模型创建一个选择模型,但我首先尝试进行一些数据准备。

数据如下:

        PANID       WEEK L5num
1669  3174607 2008-09-08     9
21356 3362137 2011-08-01     7
15540 3174607 2010-10-04     9
5869  3150938 2009-07-13     3
18401 3174607 2011-02-28     9
10616 3174607 2010-03-22     8

我正在尝试构建数据,以使ID 3174607选择产品9等。我正在尝试以类似于以下格式的格式放置数据:

PANID        Product
3174607        0
3174607        0
3174607        0
3174607        0
3174607        0
3174607        0
3174607        0
3174607        0
3174607        0
3174607        1
3174607        0
…
Next PANID

因此该人从10种产品中选择了9种。

希望这是有道理的,任何帮助将不胜感激

编辑:

非常感谢您的答复。我已经使用了您所有的答案,并且都可以使用。我的原始帖子确实有一点“添加”。我也希望reshape我的数据。我的数据有x个变量,我在原始帖子中没有讨论。

数据看起来像

PANID WEEK  L5num X1 X2 X3 X4...

是否可以应用您建议的方法同时包含x变量?因此输出将与当前建议的相同,但RHS上带有x变量 编辑2:

我用“ x”变量添加相同的数据

structure(list(PANID = c(3362137L, 3109124L, 3345264L, 3174607L, 
3174607L, 3169102L, 3322008L, 3174607L, 3800607L, 3368878L, 3174607L, 
3174607L, 3340018L, 3109124L, 3174607L, 3338756L, 3842948L, 3107920L, 
3174607L, 3369801L, 3174607L, 3174607L, 3330266L, 3107920L, 3831099L, 
3837682L, 3174607L, 3174607L, 3174607L, 3174607L, 3174607L, 3174607L, 
3174607L, 3174607L, 3330266L, 3174607L, 3340299L, 3368936L, 3317974L, 
3174607L, 3322107L, 3174607L, 3164467L, 3174607L, 3140616L, 3314419L, 
3174607L, 3369801L, 3174607L, 3375709L, 3361931L, 3174607L, 3160101L, 
3174607L, 3174607L, 3369801L, 3117754L, 3174607L, 3174607L, 3174607L, 
3174607L, 3114728L, 3174607L, 3369801L, 3109124L, 3379453L, 3819300L, 
3814491L, 3340547L, 3109124L, 3821025L, 3174607L, 3174607L, 3147447L, 
3174607L, 3317578L, 3341081L, 3140418L, 3342014L, 3174607L, 3174607L, 
3174607L, 3174607L, 3174607L, 3174607L, 3309476L, 3174607L, 3161760L, 
3174607L, 3181172L, 3174607L, 3174607L, 3164467L, 3177436L, 3174607L, 
3814533L, 3174607L, 3107334L, 3174607L, 3174607L, 3174607L, 3174607L, 
3174607L, 3174607L, 3159525L, 3107334L, 3153767L, 3362137L, 3174607L, 
3174607L, 3174607L, 3174607L, 3308189L, 3174607L, 3300483L, 3174607L, 
3174607L, 3174607L, 3341206L, 3301655L, 3343145L, 3174607L, 3821710L, 
3348904L, 3174607L, 3174607L, 3174607L, 3174607L, 3338764L, 3176552L, 
3372581L, 3822940L, 3340018L, 3338764L, 3174607L, 3174607L, 3174607L, 
3174607L, 3174607L, 3164467L, 3174607L, 3128389L, 3362137L, 3153239L, 
3174607L, 3174607L, 3162446L, 3328948L, 3340299L, 3174607L, 3164467L, 
3341206L, 3832469L, 3396135L, 3308577L, 3174607L, 3174607L, 3174607L, 
3174607L, 3174607L, 3174607L, 3174607L, 3174607L, 3106344L, 3141044L, 
3174607L, 3161760L, 3402438L, 3800607L, 3174607L, 3174607L, 3339374L, 
3330613L, 3820431L, 3174607L, 3174607L, 3174607L, 3362137L, 3174607L, 
3301655L, 3107334L, 3174607L, 3164467L, 3817668L, 3174607L, 3114298L, 
3174607L, 3174607L, 3174607L, 3174607L, 3174607L, 3340018L, 3174607L, 
3164467L, 3174607L, 3154336L, 3802355L, 3340018L, 3174607L, 3174607L, 
3174607L, 3402263L, 3174607L, 3174607L, 3174607L, 3173872L, 3339366L, 
3837740L, 3174607L, 3174607L, 3309096L, 3307926L, 3153767L, 3174607L, 
3107334L, 3174607L, 3164467L, 3340018L, 3174607L, 3113837L, 3174607L, 
3340018L, 3174607L, 3174607L, 3174607L, 3801282L, 3174607L, 3337006L, 
3174607L, 3330266L, 3174607L, 3340018L, 3188888L, 3174607L, 3174607L, 
3340018L, 3834762L, 3177436L, 3107334L, 3402263L, 3174607L, 3174607L, 
3174607L, 3174607L, 3108613L, 3174607L, 3340018L, 3174607L, 3174607L, 
3340547L, 3174607L, 3174607L, 3362137L, 3174607L, 3340018L, 3164467L, 
3819482L, 3379453L, 3160101L, 3801365L, 3340018L, 3174607L, 3174607L, 
3174607L, 3174607L, 3174607L, 3312850L, 3174607L, 3174607L, 3174607L, 
3402263L, 3340018L, 3348904L, 3174607L, 3174607L, 3350173L, 3301655L, 
3117333L, 3174607L, 3174607L, 3147447L, 3107334L, 3812628L, 3837740L, 
3837740L, 3132209L, 3174607L, 3174607L, 3308890L, 3310235L, 3361931L, 
3118554L, 3330266L, 3308189L, 3157099L, 3817767L, 3174607L, 3368936L, 
3362137L, 3157099L, 3814533L, 3322404L, 3174607L, 3174607L, 3174607L, 
3330266L, 3315424L, 3161430L, 3174607L, 3174607L, 3174607L, 3174607L, 
3802355L, 3174607L, 3174607L, 3174607L, 3174607L, 3153767L, 3335679L, 
3174607L, 3831644L, 3313304L, 3339374L, 3160101L, 3822940L, 3141044L, 
3369801L, 3330266L, 3174607L, 3336347L, 3833129L, 3174607L, 3164640L, 
3402263L, 3174607L, 3138511L, 3817767L, 3174607L, 3174607L, 3159517L, 
3106344L, 3174607L, 3147447L, 3174607L, 3368936L, 3176552L, 3346551L, 
3174607L, 3174607L, 3174607L, 3174607L, 3362137L, 3174607L, 3369801L, 
3174607L, 3136226L, 3174607L, 3174607L, 3174607L, 3164467L, 3174607L, 
3157099L, 3174607L, 3174607L, 3174607L, 3174607L, 3377192L, 3147447L, 
3174607L, 3169771L, 3106344L, 3174607L, 3174607L, 3842948L, 3174607L, 
3340018L, 3147447L, 3330266L, 3174607L, 3174607L, 3174607L, 3169771L, 
3819482L, 3174607L, 3317511L, 3174607L, 3174607L, 3174607L, 3174607L, 
3340018L, 3174607L, 3174607L, 3174607L, 3814533L, 3164467L, 3131490L, 
3375709L, 3164467L, 3335679L, 3339366L, 3164467L, 3139105L, 3174607L, 
3164467L, 3301655L, 3174607L, 3171066L, 3174607L, 3147447L, 3174607L, 
3174607L, 3174607L, 3174607L, 3340018L, 3356063L, 3314567L, 3838516L, 
3174607L, 3174607L, 3174607L, 3341206L, 3339366L, 3356964L, 3174607L, 
3174607L, 3369801L, 3812750L, 3176552L, 3174607L, 3348904L, 3114298L, 
3174607L, 3835934L, 3325381L, 3174607L, 3164467L, 3317974L, 3174607L, 
3174607L, 3174607L, 3330266L, 3153239L, 3362137L, 3340018L, 3339366L, 
3821710L, 3174607L, 3174607L, 3330266L, 3174607L, 3174607L, 3340018L, 
3174607L, 3173575L, 3174607L, 3160101L, 3174607L, 3118562L, 3174607L, 
3814491L, 3174607L, 3396788L, 3109124L, 3398057L, 3153767L, 3825836L, 
3174607L, 3174607L, 3308080L, 3174607L, 3164467L, 3308189L, 3114728L, 
3301655L, 3174607L, 3337337L, 3174607L, 3837211L, 3174607L, 3335810L, 
3174607L, 3174607L, 3174607L, 3174607L, 3160101L, 3315424L, 3348904L, 
3308080L, 3317974L, 3164467L, 3174607L, 3174607L, 3800607L, 3402263L, 
3174607L, 3174607L, 3106344L, 3174607L, 3815084L, 3308080L), 
    WEEK = structure(c(14788, 14781, 14543, 15005, 14753, 14074, 
    14116, 14599, 14970, 14781, 14928, 14676, 15565, 15033, 14809, 
    14109, 14571, 14207, 14543, 14956, 14767, 14774, 14795, 15635, 
    14193, 15215, 14417, 14802, 14214, 15089, 14690, 14592, 14487, 
    14935, 15537, 14823, 15152, 15264, 14417, 14774, 14193, 14424, 
    15467, 14340, 15565, 13878, 15068, 15593, 15047, 14312, 14032, 
    14669, 14578, 14739, 15131, 15481, 14298, 14487, 15103, 15124, 
    15012, 15012, 14935, 14928, 15194, 15208, 14697, 15131, 14984, 
    14403, 14347, 15033, 14571, 13941, 14690, 15173, 15194, 14382, 
    14060, 14774, 14403, 14676, 14319, 14837, 14921, 14452, 14487, 
    15432, 14676, 14466, 14830, 14585, 14711, 14837, 15110, 14011, 
    14767, 14795, 14949, 14942, 15033, 14445, 14970, 15033, 14963, 
    14781, 14690, 14172, 14452, 14760, 14816, 14431, 14774, 15103, 
    14536, 14550, 14704, 14879, 15495, 14697, 15145, 14515, 15215, 
    14515, 15089, 14739, 14445, 15047, 15236, 15376, 15201, 15131, 
    14949, 14550, 14578, 15082, 14872, 14375, 15124, 14690, 15600, 
    14368, 15432, 14116, 14893, 14851, 14060, 14809, 14620, 15054, 
    15460, 15572, 13990, 15320, 14746, 14760, 14739, 15089, 14830, 
    15089, 15033, 14942, 14165, 14655, 14053, 14676, 14046, 14816, 
    14060, 14774, 14165, 14844, 15124, 15005, 14480, 15257, 14592, 
    14522, 14501, 14340, 14830, 14081, 14340, 15250, 14753, 14417, 
    14837, 14690, 15236, 15047, 15047, 15117, 14711, 15460, 15131, 
    15411, 14725, 14410, 14515, 14669, 14809, 15509, 14557, 14837, 
    15026, 14382, 14228, 14431, 15236, 14760, 14466, 14697, 15201, 
    14445, 14445, 14767, 14606, 14928, 14193, 15579, 15068, 14165, 
    14529, 15089, 14865, 15110, 14816, 14704, 14690, 15586, 14837, 
    14319, 14697, 14690, 14991, 13983, 15523, 13906, 14200, 15327, 
    14956, 14774, 14676, 14417, 15159, 14942, 15110, 14949, 14739, 
    15341, 14578, 14655, 14823, 14851, 15397, 15579, 13955, 15145, 
    14221, 15418, 15537, 14697, 14389, 14928, 15040, 14851, 13927, 
    14389, 14893, 14396, 15278, 14333, 13885, 14305, 14928, 14767, 
    15075, 15054, 14690, 15033, 14081, 15145, 15565, 14123, 14410, 
    13899, 15131, 14830, 14207, 14949, 15180, 15614, 14515, 15369, 
    15201, 15278, 14760, 14655, 14424, 15026, 14144, 15516, 14935, 
    14858, 14480, 14571, 14942, 14102, 14242, 14536, 14753, 14088, 
    15292, 14410, 14445, 14963, 14298, 15250, 14473, 14501, 14935, 
    14599, 15264, 14767, 15124, 13941, 14179, 14977, 14529, 14837, 
    14067, 14459, 14718, 14788, 14445, 14102, 15313, 14725, 14319, 
    14004, 14571, 14928, 14501, 14676, 15432, 15439, 15061, 14459, 
    14151, 14410, 15047, 14172, 14662, 14816, 14963, 15271, 14704, 
    14816, 14816, 14865, 15061, 14445, 15215, 14879, 14480, 14347, 
    14396, 14396, 14956, 13983, 14746, 14788, 14837, 14501, 14739, 
    14487, 13892, 14606, 14809, 14802, 14879, 13983, 14438, 14739, 
    14669, 14403, 15250, 14774, 15257, 15306, 14739, 15131, 14396, 
    14137, 14844, 15033, 15299, 14494, 14963, 14368, 15411, 14270, 
    14606, 15404, 14081, 14830, 14515, 14116, 14529, 14445, 15089, 
    14683, 15054, 14480, 15425, 15467, 14564, 15250, 14277, 15271, 
    15551, 15509, 15467, 14641, 14711, 14375, 15572, 15488, 15124, 
    14914, 15341, 14641, 14662, 14179, 14410, 15460, 14466, 14683, 
    14130, 14578, 15152, 14963, 13955, 14984, 14809, 14312, 14144, 
    14830, 14123, 14851, 14837, 14564, 15047, 15614, 14739, 14571, 
    14424, 13962, 14802, 14816, 14949, 14410, 14592, 15593, 15005, 
    14641, 14389, 14760, 14039, 14515, 15369, 15054, 15152, 14515, 
    14165, 14032, 15117, 15138, 14984, 13934, 15005, 14963, 15096, 
    14795, 14473, 14228, 15061, 14410, 15152, 14683, 14452, 14459, 
    15138, 15425, 14452, 14571, 15334, 14403, 14816, 14452), class = "Date"), 
    L5num = c(7, 3, 7, 9, 8, 9, 2, 9, 6, 2, 8, 9, 6, 3, 8, 2, 
    6, 6, 9, 2, 8, 8, 10, 6, 7, 2, 9, 9, 9, 9, 9, 8, 9, 8, 10, 
    9, 6, 6, 7, 8, 2, 9, 4, 9, 1, 7, 9, 9, 9, 4, 7, 9, 7, 9, 
    9, 9, 7, 9, 9, 9, 9, 7, 9, 9, 3, 10, 6, 6, 7, 3, 5, 9, 9, 
    6, 9, 1, 2, 7, 2, 9, 8, 9, 9, 8, 9, 2, 9, 7, 8, 9, 8, 9, 
    4, 7, 9, 10, 8, 7, 9, 9, 8, 8, 9, 9, 1, 7, 10, 7, 8, 9, 9, 
    8, 2, 9, 7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, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 2L, 4L, 2L, 3L, 2L, 2L, 2L, 
    4L, 4L, 2L, 4L, 2L, 2L, 2L, 3L, 4L, 2L, 4L, 1L, 4L, 2L, 4L, 
    4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 2L, 2L, 4L, 4L, 
    1L, 4L, 2L, 2L)), row.names = c(13036L, 12834L, 8179L, 17887L, 
11867L, 1036L, 1541L, 9145L, 17275L, 12897L, 16092L, 10428L, 
24672L, 18295L, 13566L, 1454L, 8757L, 2361L, 8106L, 16966L, 12331L, 
12587L, 13236L, 25163L, 2311L, 21600L, 5418L, 13442L, 2482L, 
19924L, 10710L, 8999L, 7010L, 16277L, 24453L, 14022L, 21067L, 
22195L, 5502L, 12573L, 2288L, 5604L, 23753L, 3936L, 24638L, 17L, 
19433L, 24874L, 18797L, 3601L, 731L, 10239L, 8776L, 11645L, 20797L, 
23951L, 3323L, 7019L, 20090L, 20535L, 18087L, 18058L, 16363L, 
16227L, 21366L, 21523L, 10931L, 20839L, 17567L, 5003L, 4147L, 
18394L, 8676L, 305L, 10690L, 21231L, 21397L, 4546L, 962L, 12683L, 
5052L, 10406L, 3620L, 14374L, 15971L, 6406L, 6999L, 23429L, 10349L, 
6652L, 14117L, 8948L, 11088L, 14528L, 20269L, 638L, 12357L, 13066L, 
16764L, 16591L, 18383L, 6102L, 17188L, 18414L, 16997L, 12821L, 
10597L, 2122L, 6308L, 12210L, 13760L, 5748L, 12782L, 20081L, 
8016L, 8248L, 11025L, 15327L, 24064L, 10911L, 20996L, 7692L, 
21597L, 7745L, 19813L, 11663L, 6159L, 18819L, 21814L, 23107L, 
21478L, 20845L, 16815L, 8336L, 8823L, 19690L, 15281L, 4449L, 
20591L, 10605L, 24922L, 4318L, 23481L, 1493L, 15628L, 14853L, 
946L, 13659L, 9496L, 18990L, 23672L, 24721L, 527L, 22688L, 11805L, 
12111L, 11691L, 19908L, 14248L, 19843L, 18365L, 16503L, 2017L, 
9918L, 874L, 10440L, 827L, 13863L, 967L, 12732L, 1994L, 14707L, 
20612L, 18043L, 6827L, 22131L, 9024L, 7828L, 7343L, 3950L, 14063L, 
1110L, 3899L, 22080L, 11907L, 5305L, 14319L, 10637L, 21792L, 
18845L, 18881L, 20431L, 11169L, 23688L, 20748L, 23301L, 11473L, 
5283L, 7652L, 10263L, 13525L, 24187L, 8407L, 14415L, 18256L, 
4568L, 2703L, 5858L, 21788L, 12242L, 6657L, 10913L, 21440L, 6220L, 
6013L, 12410L, 9253L, 16220L, 2251L, 24751L, 19446L, 2042L, 7895L, 
19871L, 15067L, 20311L, 13733L, 11064L, 10699L, 24831L, 14384L, 
3668L, 10907L, 10721L, 17641L, 505L, 24340L, 153L, 2317L, 22759L, 
16837L, 12497L, 10439L, 5441L, 21121L, 16457L, 20295L, 16668L, 
11591L, 22842L, 8801L, 9931L, 14046L, 14792L, 23242L, 24765L, 
411L, 21002L, 2562L, 23375L, 24459L, 10865L, 4812L, 16165L, 18617L, 
14819L, 236L, 4821L, 15606L, 4896L, 22410L, 3859L, 56L, 3491L, 
16158L, 12457L, 19609L, 18955L, 10644L, 18450L, 1106L, 20967L, 
24685L, 1632L, 5300L, 121L, 20697L, 14178L, 2431L, 16808L, 21305L, 
24995L, 7735L, 23059L, 21441L, 22418L, 12201L, 9991L, 5666L, 
18193L, 1839L, 24240L, 16265L, 15011L, 6818L, 8733L, 16602L, 
1343L, 2798L, 8000L, 11989L, 1187L, 22516L, 5246L, 6099L, 17059L, 
3358L, 21960L, 6742L, 7378L, 16424L, 9194L, 22187L, 12320L, 20656L, 
295L, 2166L, 17417L, 7872L, 14536L, 1010L, 6495L, 11245L, 13047L, 
6193L, 1336L, 22650L, 11430L, 3642L, 565L, 8633L, 16090L, 7322L, 
10444L, 23485L, 23523L, 19310L, 6512L, 1868L, 5248L, 18802L, 
2123L, 10045L, 13852L, 17041L, 22224L, 10978L, 13730L, 13770L, 
15062L, 19198L, 6026L, 21553L, 15423L, 6839L, 4018L, 4978L, 4870L, 
16916L, 498L, 11749L, 12968L, 14404L, 7440L, 11613L, 7059L, 81L, 
9306L, 13621L, 13317L, 15421L, 500L, 5987L, 11600L, 10284L, 5096L, 
22032L, 12712L, 22102L, 22614L, 11572L, 20781L, 4902L, 1771L, 
14573L, 18304L, 22586L, 7154L, 17097L, 4391L, 23305L, 3066L, 
9292L, 23270L, 1126L, 14250L, 7625L, 1530L, 7855L, 6085L, 19921L, 
10537L, 19031L, 6887L, 23405L, 23776L, 8630L, 22017L, 3156L, 
22252L, 24574L, 24172L, 23783L, 9755L, 11180L, 4520L, 24732L, 
23985L, 20552L, 15878L, 22799L, 9769L, 10146L, 2155L, 5204L, 
23678L, 6665L, 10509L, 1670L, 8797L, 21056L, 16989L, 407L, 17565L, 
13666L, 3603L, 1795L, 14158L, 1616L, 14834L, 14521L, 8595L, 18860L, 
25015L, 11646L, 8649L, 5601L, 420L, 13286L, 13864L, 16643L, 5285L, 
8979L, 24880L, 17801L, 9825L, 4817L, 12218L, 771L, 7662L, 23046L, 
19119L, 21016L, 7726L, 2004L, 728L, 20379L, 20958L, 17549L, 283L, 
17822L, 17009L, 20008L, 13209L, 6725L, 2685L, 19311L, 5273L, 
21046L, 10471L, 6348L, 6464L, 20940L, 23410L, 6335L, 8679L, 22772L, 
5089L, 13868L, 6397L), class = "data.frame")

5 个答案:

答案 0 :(得分:2)

这里是使用tabulatedata.table的选项

library(data.table)
setDT(dat)
n <- dat[, uniqueN(L5num)] # 10
out <- dat[, .(Product = tabulate(L5num, n)), by = PANID]
out #                                    ^ total number of products

为说明起见,我使用一个小例子。

输入:

(dt <- as.data.table(head(unique(dat[, c("PANID", "L5num")]), 2)))
#     PANID L5num
#1: 3801365     4
#2: 3174607     9

输出:

out <- dt[, .(Product = tabulate(L5num, 10)), by = PANID]
out
#      PANID Product
# 1: 3801365       0
# 2: 3801365       0
# 3: 3801365       0
# 4: 3801365       1
# 5: 3801365       0
# 6: 3801365       0
# 7: 3801365       0
# 8: 3801365       0
# 9: 3801365       0
#10: 3801365       0
#11: 3174607       0
#12: 3174607       0
#13: 3174607       0
#14: 3174607       0
#15: 3174607       0
#16: 3174607       0
#17: 3174607       0
#18: 3174607       0
#19: 3174607       1
#20: 3174607       0

编辑

如果需要将此操作应用于多个列,则需要做

n <- dat[, uniqueN(L5num)] # 10
cols <- setdiff(names(dat), c("PANID", "WEEK")) # don't apply function to "WEEK" column
out <- dat[, lapply(.SD, tabulate, nbins = n), by = PANID, .SDcols = cols]

编辑2

我不确定预期的输出是什么,但是如果要扩展所有“ x”列,则可以使用联接。

输入:

(dt <- as.data.table(head(unique(dat), 2)))
#     PANID       WEEK L5num Family.Size
#1: 3362137 2010-06-28     7           2
#2: 3109124 2010-06-21     3           3 

现在计算并与“ PANID”上的dt合并

out <- dt[dt[, .(Product = tabulate(L5num, n)), by = PANID], on = "PANID"
          ][, L5num := NULL] # removes column L5num
out
#      PANID       WEEK Family.Size Product
# 1: 3362137 2010-06-28           2       0
# 2: 3362137 2010-06-28           2       0
# 3: 3362137 2010-06-28           2       0
# 4: 3362137 2010-06-28           2       0
# 5: 3362137 2010-06-28           2       0
# 6: 3362137 2010-06-28           2       0
# 7: 3362137 2010-06-28           2       1
# 8: 3362137 2010-06-28           2       0
# 9: 3362137 2010-06-28           2       0
#10: 3362137 2010-06-28           2       0
#11: 3109124 2010-06-21           3       0
#12: 3109124 2010-06-21           3       0
#13: 3109124 2010-06-21           3       1
#14: 3109124 2010-06-21           3       0
#15: 3109124 2010-06-21           3       0
#16: 3109124 2010-06-21           3       0
#17: 3109124 2010-06-21           3       0
#18: 3109124 2010-06-21           3       0
#19: 3109124 2010-06-21           3       0
#20: 3109124 2010-06-21           3       0

答案 1 :(得分:1)

如果Data是输入data.frame,则:

library(mlogit)
mlogit.data(Data, choice = "L5num", shape = "wide")

答案 2 :(得分:0)

我们还可以使用以下基本R方法:

n <- 10 # Number of products
data.frame(PANID = rep(df$PANID, each = n), 
           Product = replace(numeric(n * nrow(df)), 0:(nrow(df) - 1) * n + df$L5num, 1))
#        PANID Product
# 1    3801365       0
# 2    3801365       0
# 3    3801365       0
# 4    3801365       1
# 5    3801365       0
...

我们从Product列开始,以numeric(n * nrow(df))给出的零向量开始,然后在0:(nrow(df) - 1) * n + df$L5num的所需位置(replace)处添加一个。


如果还有更多变量,我们可以做

data.frame(PANID = rep(df$PANID, each = n), 
           Product = replace(numeric(n * nrow(df)), 0:(nrow(df) - 1) * n + df$L5num, 1),
           df[rep(1:nrow(df), each = n), -1:-3, drop = FALSE])
#           PANID Product Family.Size
# 13036   3362137       0           2
# 13036.1 3362137       0           2
# 13036.2 3362137       0           2

其中,每个n的多余变量将重复PANID次。如果变量多于一个,则不需要drop = FALSE

答案 3 :(得分:0)

使用dummyVars软件包-

get_queryset

答案 4 :(得分:0)

使用tidyverse,您可以执行以下操作:

df %>%
 do(data.frame(PANID = rep(.$PANID, each = 10), #Replicating "PANID" and "L5num" 10x
               L5num = rep(.$L5num, each = 10))) %>%
 mutate(temp = gl(length(PANID)/10, 10)) %>% #For all "PANID" values generating an ID
 group_by(temp) %>% #Grouping by ID
 mutate(rowid = seq_along(temp)) %>% #Sequencing around ID to create a row ID
 mutate(Product = ifelse(rowid == L5num, 1, 0)) %>% #Checking whether the row ID is equal to "L5num"
 ungroup() %>%
 select(-rowid,-L5num,-temp) #Removing the redundant variables

     PANID Product
     <int>   <dbl>
 1 3174607      0.
 2 3174607      0.
 3 3174607      0.
 4 3174607      0.
 5 3174607      0.
 6 3174607      0.
 7 3174607      0.
 8 3174607      0.
 9 3174607      1.
10 3174607      0.
11 3362137      0.
12 3362137      0.
13 3362137      0.
14 3362137      0.
15 3362137      0.
16 3362137      0.
17 3362137      1.
18 3362137      0.
19 3362137      0.
20 3362137      0.