r - Add jitter to column value using dplyr -
i have data frame of following format.
author year stages 1 1150 1 2 b 1200 1 3 c 1200 1 4 d 1300 1 5 d 1300 1 6 e 1390 3 7 f 1392 3 8 g 1400 3 9 g 1400 3 ...
i want jitter each year , author combination small amount. want documents different authors in same year jittered unique values. example, tokens author b , c appear in same year, should jittered different amounts. tokens same author, example 2 tokens author g @ 1400 should jittered same amount.
i've tried following, unique jitter amount each , every row.
data %>% group_by(author) %>% mutate(year = jitter(year, amount=.5))
the output of code following.
author year stages 1 1150.400 1 2 b 1200.189 1 3 c 1200.222 1 4 d 1300.263 1 5 d 1299.788 1 6 e 1390.045 3 7 f 1391.964 3 8 g 1399.982 3 9 g 1399.783 3
however, following, both tokens author g should shifted same amount. crucial difference author g tokens shifted same amount.
author year stages 1 1150.400 1 2 b 1200.189 1 3 c 1200.222 1 4 d 1300.263 1 5 d 1299.788 1 6 e 1390.045 3 7 f 1391.964 3 8 g 1399.982 3 9 g 1399.982 3
calculate jitter 1 case , add difference cases:
dat %>% group_by(author) %>% mutate(year = year + (year[1] - jitter(year[1], amount=.5))) # author year stages #1 1149.720 1 #2 b 1200.385 1 #3 c 1199.888 1 #4 d 1299.589 1 #5 d 1299.589 1 #6 e 1389.866 3 #7 f 1392.225 3 #8 g 1400.147 3 #9 g 1400.147 3
Comments
Post a Comment