Transform Data in R -
i have uploaded data set r. dataset has 2 columns
user_id merchant_id 514729 14852,16695 1240327 23590 7457 211 359027 2483 463149 5802 514730 5460,1896 41953 7183,147105 927805 304,3909,4151,32,3,39171
as can see user ids associated multiple merchants. looking transform data in such way have following schema
user id merchantid1 merchantid2 merchantid3 merchantid 4 123445 0 1 0 1 123453 1 0 0 0
basically want create matrix of userid , merchant ids 1 or 0 based on if user_id has merchant_id or not.
any suggestions/help on how can accomplish this?
i looking use build recommendation system. great.
my interpretation after following:
library(splitstackshape) csplit_e(mydf, "merchant_id", ",", type = "character", fill = 0) ## user_id merchant_id merchant_id_147105 merchant_id_14852 merchant_id_16695 ## 1 514729 14852,16695 0 1 1 ## 2 1240327 23590 0 0 0 ## 3 7457 211 0 0 0 ## 4 359027 2483 0 0 0 ## 5 463149 5802 0 0 0 ## 6 514730 5460,1896 0 0 0 ## 7 41953 7183,147105 1 0 0 ## merchant_id_1896 merchant_id_211 merchant_id_23590 merchant_id_2483 ## 1 0 0 0 0 ## 2 0 0 1 0 ## 3 0 1 0 0 ## 4 0 0 0 1 ## 5 0 0 0 0 ## 6 1 0 0 0 ## 7 0 0 0 0 ## merchant_id_5460 merchant_id_5802 merchant_id_7183 ## 1 0 0 0 ## 2 0 0 0 ## 3 0 0 0 ## 4 0 0 0 ## 5 0 1 0 ## 6 1 0 0 ## 7 0 0 1
Comments
Post a Comment