I have data as below.
names=c(rep("a",4),rep("b",5),rep("c",2))
time=c(1,2,3,4,1,2,3,4,5,1,2)
dd=data.frame(names,time)
dd <- group_by(dd, names)
dd <- mutate(dd, seq=seq_along(names))
extr <- summarise(dd, minw=min(time), maxw=max(time))
> dd
Source: local data frame [11 x 3]
Groups: names
names time seq
1 a 1 1
2 a 2 2
3 a 3 3
4 a 4 4
5 b 1 1
6 b 2 2
7 b 3 3
8 b 4 4
9 b 5 5
10 c 1 1
11 c 2 2
> extr
Source: local data frame [3 x 3]
names minw maxw
1 a 1 4
2 b 1 5
3 c 1 2
The final output that I need is as below. I want to add two columns - first_indicator and last_indicator which will have value "yes" if combination of name and sequence have the first and last value respectively. How could i do it using dd and extr dataframes that i am generating above?
names time seq first_indicator last_indicator
1 a 1 1 yes
2 a 2 2
3 a 3 3
4 a 4 4 yes
5 b 1 1 yes
6 b 2 2
7 b 3 3
8 b 4 4
9 b 5 5 yes
10 c 1 1 yes
11 c 2 2 yes
In base R, use
ave
:Using
data.table
you could do something like: