Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observation number by group [duplicate]

Tags:

r

sas

In R I have a data frame with observations described by several values one of which is a factor. I have sorted the dataset by this factor and would like to add a column in which I would get a number of observation on each level of the factor e.g.

factor   obsnum
a        1
a        2
a        3
b        1
b        2
b        3
b        4
c        1
c        2
...

In SAS I do it with something like:

data logs.full;
    set logs.full;
    count + 1;
    by cookie;
    if first.cookie then count = 1;
run;

How can I achieve that in R?

Thanks,

like image 541
twowo Avatar asked Nov 21 '11 08:11

twowo


People also ask

How do I remove duplicates by GROUP BY?

To delete the duplicate rows from the table in SQL Server, you follow these steps: Find duplicate rows using GROUP BY clause or ROW_NUMBER() function. Use DELETE statement to remove the duplicate rows.

Does GROUP BY clause remove duplicates?

GROUP BY does not remove duplicates.

Does GROUP BY return duplicates?

GROUP BY only treats two rows as duplicates if all the column values in both the rows are the same. If even a single column value in either of the row is non-matching, they are treated as unique.


1 Answers

Use rle (run length encoding) and sequence:

x <- c("a", "a", "a", "b", "b", "b", "b", "c", "c")

data.frame(
    x=x,
    obsnum = sequence(rle(x)$lengths) 
)

  x obsnum
1 a      1
2 a      2
3 a      3
4 b      1
5 b      2
6 b      3
7 b      4
8 c      1
9 c      2
like image 164
Andrie Avatar answered Oct 30 '22 17:10

Andrie