In R, I'm trying to get the frequency and the first occurence for each item of a column in a data frame.
I have this:
df_input <- data.frame(observation_source = c("AB","CD","EF","GH","IJ","KL","MN"), observation_value = c(15,17,17,17,21,32,32))
observation_source observation_value
1 AB 15
2 CD 17
3 EF 17
4 GH 17
5 IJ 21
6 KL 32
7 MN 32
And I'm trying to get this:
observation_source observation_value value_frequency value_first_row
1 AB 15 1 1
2 CD 17 3 2
3 EF 17 3 2
4 GH 17 3 2
5 IJ 21 1 5
6 KL 32 2 6
7 MN 32 2 6
Such that, on row 4 for example, the value 17 occurs 3 times in total and occurs on row 2 for the first time.
I know how to do this with a for loop, but it gets extremely slow as the number of row increases (e.g. 100,000). Any idea how else I can do that? Many thanks!!