R count occurrences of an element by groups [duplicate]
问题 This question already has answers here : Add column with order counts (2 answers) Count number of rows within each group (15 answers) Closed 5 years ago . What is the easiest way to count the occurrences of a an element on a vector or data.frame at every grouop? I don't mean just counting the total (as other stackoverflow questions ask) but giving a different number to every succesive occurence. for example for this simple dataframe: (but I will work with dataframes with more columns) mydata