I have a data frame that looks like this:
master_bill_no category
SBA5100008 CONDOMS
SBA5100008 HAND CREAM
SBA5100009 PREGNANCY TESTS
SBA5100010 MULTI VITAMINS & MIN
SBA5100010 CALCIUM PREPARATIONS
SBA5100010 VITAMINS
SBA5100010 BETABLOCKERS
a reproducible example is given below:
structure(list(master_bill_no = c("SBA5100008", "SBA5100008",
"SBA5100009", "SBA5100010", "SBA5100010", "SBA5100010", "SBA5100010"
), category = c("CONDOMS", "HAND CREAM", "PREGNANCY TESTS", "MULTI VITAMINS & MIN",
"CALCIUM PREPARATIONS", "VITAMINS", "BETABLOCKERS")), .Names = c("master_bill_no",
"category"), class = "data.frame", row.names = c(NA, -7L))
For each unique master bill no, i am trying to reshape the column category to a wide one.
For example, the desired output would be:
master_bill_no category
SBA5100008 CONDOMS,HAND CREAM
SBA5100009 PREGNANCY TESTS
SBA5100010 MULTI VITAMINS & MIN,CALCIUM PREPARATIONS,CALCIUM PREPARATIONS,BETABLOCKERS
I used the base reshape formula, and it just deletes the category column.
reshape(df, idvar = "master_bill_no", timevar = "category", direction = "wide")
I tried aggregate function:
aggregate(df, master_bill_no, FUN = paste(category, sep = ","))
This returns a error message "object category not found"
I am sure the reason for this is reshape is looking for values to fill which is missing. Can someone help please?