Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flag first by-group in R data frame

Tags:

r

I have a data frame which looks like this:

id  score
1   15
1   18
1   16
2   10
2   9
3   8
3   47
3   21

I'd like to identify a way to flag the first occurrence of id -- similar to first. and last. in SAS. I've tried the !duplicated function, but I need to actually append the "flag" column to my data frame since I'm running it through a loop later on. I'd like to get something like this:

id  score   first_ind
1   15      1
1   18      0
1   16      0
2   10      1
2   9       0
3   8       1
3   47      0
3   21      0
like image 555
davids12 Avatar asked Oct 08 '14 20:10

davids12


2 Answers

> df$first_ind <- as.numeric(!duplicated(df$id))
> df
  id score first_ind
1  1    15         1
2  1    18         0
3  1    16         0
4  2    10         1
5  2     9         0
6  3     8         1
7  3    47         0
8  3    21         0
like image 90
Jilber Urbina Avatar answered Nov 05 '22 23:11

Jilber Urbina


You can find the edges using diff.

x <- read.table(text = "id  score
1   15
1   18
1   16
2   10
2   9
3   8
3   47
3   21", header = TRUE)

x$first_id <- c(1, diff(x$id))
x

  id score first_id
1  1    15        1
2  1    18        0
3  1    16        0
4  2    10        1
5  2     9        0
6  3     8        1
7  3    47        0
8  3    21        0
like image 29
Roman Luštrik Avatar answered Nov 05 '22 23:11

Roman Luštrik