r - Aggregating by unique identifier and concatenating related values into a string -
this question has answer here:
i have need imagine satisfied aggregate or reshape, can't quite figure out.
i have list of names (brand), , accompanying id number (id).  data in long form, names can have multiple id's.  i'd de-dupicate name (brand) , concatenate multiple possible id's string separated comment.
for example:
brand            id  radioshack       2308 rag & bone       4466 ragu             1830 ragu             4518 ralph lauren     1638 ralph lauren     2719 ralph lauren     2720 ralph lauren     2721 ralph lauren     2722  should become:
radioshack       2308 rag & bone       4466 ragu             1830,4518 ralph lauren     1638,2719,2720,2721,2722 how accomplish this?
let's call data.frame df
> aggregate(id ~ brand, data = df, c)          brand                           id 1   radioshack                         2308 2   rag & bone                         4466 3         ragu                   1830, 4518 4 ralph lauren 1638, 2719, 2720, 2721, 2722 another alternative using aggregate is:
result <- aggregate(id ~ brand, data = df, paste, collapse = ",") this produces same result , id not list anymore. @frank comment. see class of each column try:
> sapply(result, class)       brand          id     "factor" "character" as mentioned @davidarenburg in comments, alternative using tostring function:
aggregate(id ~ brand, data = df, tostring) 
Comments
Post a Comment