I have a data frame (all_data
) in which I have a list of sites (1... to n) and their scores e.g.
site score
1 10
1 11
1 12
4 10
4 11
4 11
8 9
8 8
8 7
I want create a column that numbers each level of site in numerical order, like a counter. In the example, the sites (1, 4, and 8) would be have a corresponding counter from 1 to 3 in the 'number' column:
site score number
1 10 1
1 11 1
1 12 1
4 10 2
4 11 2
4 11 2
8 9 3
8 8 3
8 7 3
I am sure this must be easily solved, but I have not found a way yet.
Two other options:
1) Using the
.GRP
function from thedata.table
package:with the example dataset from below this results in:
2) Using the
group_indices
function fromdplyr
:or when you want to work around non-standard evaluation:
which results in:
As can be seen,
dplyr
gives a different order of the group numbers.If you want another number every time the group changes, there are several other options:
1) with base R:
2) with the
data.table
package:which all result in:
Used data:
Another solution using the
data.table
package.Example with the more complete datset provided by Jaap:
This should be fairly efficient and understandable:
You can turn site into a factor and then return the numeric or integer values of that factor:
Try
Data$number <- as.numeric(as.factor(Data$site))
On a sidenote : the difference between the solution of me and @Chase on one hand, and the one of @DWin on the other, is the ordering of the numbers. Both
as.factor
andfactor
will automatically sort the levels, whereas that doesn't happen in the solution of @DWin :Gives