Reputation: 283
I have a dataset data1 as shown below
Id Results
1232 Az
1232 Ca
1223 Hi
1223 Az
8477 Ca
8477 Ca
I used aggregate function to group these based on Id,
aggregate(Results~ Id, data=data1, FUN=head,1)
I was expecting
Id Results
1232 Az,Ca,Hi
1223 Hi, Az
8477 Ca, Ca
I am seeing
Id Results
1232 Az
1223 Az
1223 Ca
Upvotes: 1
Views: 143
Reputation: 27388
A single value per group is what you should expect if the function you pass to aggregate
is head(x, 1)
.
If instead you want a string of comma-separated values that belong to each group, you can use:
aggregate(Results ~ Id, d, paste0, collapse=',')
# Id Results
# 1 1223 Hi,Az
# 2 1232 Az,Ca
# 3 8477 Ca,Ca
Upvotes: 1