r - For n rows, subtract one number; then next set of n rows switch to a new number to subtract from -
i have series of readings within 1 large dataframe shown below:
plate_id day well_id name x590_mean x590_sd x750_mean x750_sd 1 mcba15 001 0 spl1 water 0.196 0.003 0.145 0.004 2 mcba15 001 0 spl2 pyruvic acid methyl ester 0.202 0.001 0.143 0.000 3 mcba15 001 0 spl3 tween 40 0.214 0.036 0.158 0.026 4 mcba15 001 0 spl4 tween 80 0.196 0.000 0.144 0.002 5 mcba15 001 0 spl5 ?-cyclodextrin 0.217 0.012 0.161 0.012 ... 33 mcba15 001 1 spl1 water 0.209 0.008 0.111 0.003 34 mcba15 001 1 spl2 pyruvic acid methyl ester 0.371 0.007 0.148 0.003 35 mcba15 001 1 spl3 tween 40 0.481 0.127 0.285 0.088 36 mcba15 001 1 spl4 tween 80 0.242 0.011 0.108 0.002 37 mcba15 001 1 spl5 ?-cyclodextrin 0.277 0.002 0.138 0.001
basically, need subtract mean values (590 , 750
) in water
rows each plate
, day
rows share same variables (ie. every 32 rows, switch next mean values subtract subsequent rows)
the desired output should be:
plate_id day well_id name x590_mean x590_sd x750_mean x750_sd 1 mcba15 001 0 spl1 water 0.000 0.003 0.000 0.004 2 mcba15 001 0 spl2 pyruvic acid methyl ester 0.006 0.001 0.000 0.000 3 mcba15 001 0 spl3 tween 40 0.018 0.036 0.013 0.026 ... 33 mcba15 001 1 spl1 water 0.000 0.008 0.000 0.003 34 mcba15 001 1 spl2 pyruvic acid methyl ester 0.162 0.007 0.037 0.003
any values become negative
should 0
.
i have tried following approach run lot of difficulties.
sp2 <- split(dat, with(dat, interaction(plate_id, day))) sapply(sp2, dim) d <- function(biolog) { x <- biolog$x590_mean[1] biolog$x590_mean[biolog$x590_mean > x] <- biolog$x590_mean - x y <- biolog$x750_mean[1] biolog$x750_mean[biolog$x750_mean > y] <- biolog$x750_mean - y } lapply(sp2, d)
you can try solution:
d <- function(biolog) { biolog$x590_mean <- with(biolog, x590_mean - x590_mean[1]) biolog$x590_mean <- ifelse(biolog$x590_mean < 0, 0, biolog$x590_mean) biolog$x750_mean <- with(biolog, x750_mean - x750_mean[1]) biolog$x750_mean <- ifelse(biolog$x750_mean < 0, 0, biolog$x750_mean) biolog }
(i haven't tested on actual data set though. please provide dput(dat)
if there problems solution)
Comments
Post a Comment