r - standardize time points for each individual -


i have large dataset dat this:

sub  time  1   0.16  1   0.76  1   1.15  1   1.48  1   2  1   2.98  1   3.98  1   4.96  1   8.98  1   12.13  1   12.73  2   0.18  2   0.75  2   1.16  2   1.5  2   2  2   3.05  2   3.98  2   5.01  2   8.96  2   12.1  2   12.7  2   13.1  ... 

i want standardize time points each individual starts 0. expected output should this:

sub time 1   0 1   0.6 1   0.99 1   1.32 1   1.84 1   2.82 1   3.82 1   4.8 1   8.82 1   11.97 1   12.57 2   0 2   0.57 2   0.98 2   1.32 2   1.82 2   2.87 2   3.8 2   4.83 2   8.78 2   11.92 2   12.52 2   12.92 ... 

could give ideas on how realized this? lot!

it looks want subtract first value group (if sorted, minimum value o/w). use ave this, allows apply function group , return vector of same length.

tmp <- textconnection("sub  time  1   0.16  1   0.76  1   1.15  1   1.48  1   2  1   2.98  1   3.98  1   4.96  1   8.98  1   12.13  1   12.73  2   0.18  2   0.75  2   1.16  2   1.5  2   2  2   3.05  2   3.98  2   5.01  2   8.96  2   12.1  2   12.7  2   13.1") dat <- read.table(tmp, header=true) dat$norm1 <- ave(dat$time, dat$sub, fun=function(x) x - x[1]) dat$norm2 <- ave(dat$time, dat$sub, fun=function(x) x - min(x)) 

this yields:

> head(dat)   sub time norm1 norm2 1   1 0.16  0.00  0.00 2   1 0.76  0.60  0.60 3   1 1.15  0.99  0.99 4   1 1.48  1.32  1.32 5   1 2.00  1.84  1.84 6   1 2.98  2.82  2.82 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -