I have a data set where I wanted to compute some parameters repeatedly depending on a range of variable (time). So my data set looks like this:
structure(list(A = c(25L, 25L, 25L, 25L, 25L, 25L), T = 56:61,
X = c(481.07, 487.04, 490.03, 499, 504.97, 507.96), Y = c(256.97,
256.97, 256.97, 256.97, 256.97, 256.97), V = c(4.482, 5.976,
7.47, 4.482, 5.976, 7.47), thetarad = c(0.164031585831919,
0.169139558949956, 0.171661200692621, 0.179083242584008,
0.183907246800473, 0.186289411097781), thetadeg = c(9.39831757286096,
9.69098287432395, 9.83546230358968, 10.2607139792383, 10.537109061132,
10.6735970214433), bin = structure(c(1L, 1L, 1L, 1L, 1L,
1L), .Label = c("binA", "binB", "binC", "outbin"), class = "factor")), .Names = c("A",
"T", "X", "Y", "V", "thetarad", "thetadeg", "bin"), row.names = c(NA,
6L), class = "data.frame")
And this code works perfectly for computing my parameters:
NT <- data.table(st1binned [st1binned$T<31, ], key="bin")
alox1=NT[, list(ang=length(unique(thetadeg)), len=length(T), Vm=mean(V), T=c("30s")),
by=c("A", "bin")]
I can repeatedly use this code if I want to and just changed the subset on my data to st1binned and bind it to my data set:
NT <- data.table(st1binned [st1binned$T>30 & T<61, ], key="bin")
alox1=rbind(alox1, NT[, list(ang=length(unique(thetadeg)), len=length(T), Vm=mean(V), T=c("60s")),
by=c("A", "bin")])
But is there a way to loop these functions where I can say that it should consider every 30sec of data and to change also the T variable?
edited:
Resulting data set should look like this:
structure(list(A = c(38L, 45L, 115L, 118L, 121L, 692L), bin = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("binA", "binB", "binC", "outbin"
), class = "factor"), ang = c(2L, 7L, 4L, 4L, 11L, 1L), len = c(30L,
30L, 24L, 23L, 11L, 30L), Vm = c(0.3984, 1.07771666666667, 0.465545833333333,
0.760526086956522, 4.27607272727273, 0), T = c("30s", "30s",
"30s", "30s", "30s", "30s")), .Names = c("A", "bin", "ang", "len",
"Vm", "T"), class = c("data.table", "data.frame"), row.names = c(NA,
-6L), .internal.selfref = <pointer: 0x0000000000170788>)
cut()s help. I never useddata.table, so I don't know if it'll help.