I want to take the number of unique values in a subgroup and create a column with the number of unique values filled down by the subgroup.
I've already tried this chunk of code, but it hasn't worked exactly the way I was hoping:
data$ones=1
data=transform(data,Count=ave(ones,UniqueID,FUN=sum))
This is what the data looks like:
Group UniqueID
Grp1 1-A
Grp1 1-B
Grp1 1-B
Grp1 1-C
Grp2 2-A
Grp2 2-A
Grp2 2-B
Grp3 3-A
Grp3 3-A
Grp3 3-A
This is what I want it to look like:
Group UniqueID Count
Grp1 1-A 3
Grp1 1-B 3
Grp1 1-B 3
Grp1 1-C 3
Grp2 2-A 2
Grp2 2-A 2
Grp2 2-B 2
Grp3 3-A 1
Grp3 3-A 1
Grp3 3-A 1
Any help would be appreciated.