I have a dataset that looks like this:
CATA 1 10101
CATA 2 11101
CATA 3 10011
CATB 1 10100
CATB 2 11100
CATB 3 10011
etc.
and I want to combine these different rows into a single, long row like this:
CATA 101011110110011
CATB 101001110010011
I’ve tried doing this with melt() and then dcast(), but it doesn’t seem to work. Does anyone have some simple pieces of code to do this?
Look at the
pastecommand and specifically thecollapseargument. It’s not clear what should happen if/when you have different values for the first column, so I won’t venture to guess. Update your question if you get stuck.Note that you may want to convert the data to character first to prevent leading zeros from being trimmed.
EDIT: to address multiple values for the first column
Use
plyr‘sddplyfunction which expects a data.frame as an input and a grouping variable(s). We then use the samepaste()trick as before along withsummarize().