r - operations on a variable from a data.frame with time points -
i stuck in problem , send call (i went through similar questions not find need, although seems simple) : have original dataframe 50 patients 3 time points (reproducible code below) , variable of interest called "ht". goal study variations of ht between these 3 time points, example according variable (ex here numerical variable "a").
id <- rep(c(seq(1,50,1)),3) time <- factor(rep(c("day1", "day2", "day3"), c(50,50,50)), levels=c("day1", "day2", "day3"), labels=c("day1", "day2", "day3"), ordered=true) ht <- rnorm(150, mean=30, sd=3) <- rnorm(150, mean=7, sd=10) dfrm <- cbind (id,time,ht,a) > head(dfrm) id time ht [1,] 1 1 28.64048 11.1595852 [2,] 2 1 28.30068 4.2925773 [3,] 3 1 32.51943 21.2013316 [4,] 4 1 30.66561 0.6980816 [5,] 5 1 28.92749 22.2756818 [6,] 6 1 33.82217 14.2877789
i study differences between ht values between day 1 , day2, between day 2 , day 3, between day 1 , day 3. insert calcultations form indidual data id values, , other variables. not know if better build new data.frame, guess yes. suggest simple way proceed? thank in advance. denis
library(plyr) dfrm <- data.frame(dfrm) out <- ddply(dfrm, .(id), function(x) { x <- x[order(x$time),] x$diff <- c(na, diff(x$ht)) x$diff2 <- c(na, na, diff(x$ht, 2)) x })
Comments
Post a Comment