r - Calculations on selected groups of variables -
i have dataframe variable names looking like:
a.1, a.3, a.5, a.6, a.9, a.10, a.12 b.1, b.3, b.5, b.6, b.9, b.10, b.12
and on j.
the variables' names represent assessed parameters , visit number in longitudinal study.
the dataframe contains fixed baseline parameters.
i create new variables represent changes since last visit each parameter:
delta.a.3 <- a.3 - a.1 delta.a.5 <- a.5 - a.3
and on visits parameters.
is there way perform task automatically?
here extract dataframe:
id diab age 20mpace.0 20mpace.1 20mpace.3 20mpace.5 kooskpl.0 kooskpl.1 kooskpl.3 kooskpl.5 1 9000099 0 59 1.3280 1.2946 1.3500 1.2772 100.00 88.89 80.56 83.33 2 9000296 0 69 1.3658 1.3142 na 1.3944 100.00 100.00 100.00 100.00 3 9000622 0 71 1.4305 1.5178 na na 100.00 100.00 na na 4 9000798 0 56 1.0636 1.2342 1.1969 1.1572 59.38 59.38 65.63 59.38 5 9001104 0 72 1.3924 1.3473 na na 100.00 100.00 83.33 na 6 9001400 0 75 1.6203 1.5015 1.5051 1.4264 100.00 100.00 100.00 91.67
id, diab, age - "stationary" baseline parameters. 20mpace.0, 20mpace.1, 20mpace.3, 20mpace.5 - observations of 20mpace on timepoints 0, 1, 3, 5. kooskpl.0, kooskpl.1 kooskpl.3, , kooskpl.5 - observations of kooskpl on timepoints 0, 1, 3, 5.
what do:
to calculate changes in parameters on different timepoints in comparison previous timepoint
20mpace.1-20mpace.0
20mpace.3- 20mpace.1
20mpace.5-20mpace.3
kooskpl.1 - kooskpl.0
kooskpl.3 - kooskpl.1
kooskpl.5 - kooskpl.3
to place results in corresponding columns:
delta.20mpace.1
delta.20mpace.3
delta.20mpace.5.
delta.kooskpl.1
delta.kooskpl.3
delta.kooskpl.5
to calculate changes in parameters on different timepoints in relation timepoint 0:
20mpace.1-20mpace.0
20mpace.3- 20mpace.0
20mpace.5-20mpace.0
kooskpl.1 - kooskpl.0
kooskpl.3 - kooskpl.0
kooskpl.5 - kooskpl.0
again, place results in columns:
delta0.20mpace.1
delta0.20mpace.3
delta0.20mpace.5.
delta0.kooskpl.1
delta0.kooskpl.3
delta0.kooskpl.5
i did not ask last 2 questions in first instance.
may point make loop work selectively on variables same prefix (e.g. 20mpace.0, 20mpace.1, 20mpace.3 , 20mpace.5)? there way it?
i appreciate prompt , informative comments have made! however, beginner need time process information , still not understand told me.
thanks again.
i see 2 possibilities in want, you're not clear in question. possibility #1 maxim k has assumed, diff
across each row full data.frame
. possibility #2 want diff
across each row by group ("a" "j").
before start, here's sample data. i've done groups "a" , "b".
set.seed(1) mydf <- data.frame(matrix(sample(100, 50, replace = true), ncol = 10)) names(mydf) <- paste(rep(c("a", "b"), each = 5), c(1, 3, 5, 7, 9), sep = ".") mydf # a.1 a.3 a.5 a.7 a.9 b.1 b.3 b.5 b.7 b.9 # 1 27 90 21 50 94 39 49 67 83 79 # 2 38 95 18 72 22 2 60 80 65 3 # 3 58 67 69 100 66 39 50 11 79 48 # 4 91 63 39 39 13 87 19 73 56 74 # 5 21 7 77 78 27 35 83 42 53 70
possibility #1
mydf[-1] - mydf[-length(mydf)] # a.3 a.5 a.7 a.9 b.1 b.3 b.5 b.7 b.9 # 1 63 -69 29 44 -55 10 18 16 -4 # 2 57 -77 54 -50 -20 58 20 -15 -62 # 3 9 2 31 -34 -27 11 -39 68 -31 # 4 -28 -24 0 -26 74 -68 54 -17 18 # 5 -14 70 1 -51 8 48 -41 11 17
possibility #2
lapply(letters[1:2], function(x) { temp <- mydf[grepl(paste("^", x, sep = ""), names(mydf))] temp[-1] - temp[-length(temp)] }) # [[1]] # a.3 a.5 a.7 a.9 # 1 63 -69 29 44 # 2 57 -77 54 -50 # 3 9 2 31 -34 # 4 -28 -24 0 -26 # 5 -14 70 1 -51 # # [[2]] # b.3 b.5 b.7 b.9 # 1 10 18 16 -4 # 2 58 20 -15 -62 # 3 11 -39 68 -31 # 4 -68 54 -17 18 # 5 48 -41 11 17
of course, started, because i'm not @ clear on end goal here.
Comments
Post a Comment