So, for example, I have the following dataframe, data:
| col1 | col2 |
|---|---|
| 1 | 5 |
| 1 | 5 |
| 1 | 3 |
| 2 | 10 |
| 2 | 11 |
| 3 | 11 |
Now, I want to make a new column, col3, which gives me the number of unique values in col2 for every grouping in col1.
So far, I have the following code:
length(unique(data$col2[data$col1 == 1]))
Which would here return the number 2.
However, I'm having a hard time making a loop that goes through all the values in col1 to create the new column, col3.