0

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>)
1
  • Take a look on cut()s help. I never used data.table, so I don't know if it'll help. Commented Feb 3, 2014 at 16:22

1 Answer 1

2

I think you want your data to be binned in 30 second increments. If that's what you want you can do it with cut. In order to demonstrate, I had to create some data that had more than one 30 second interval. I just copied your data frame three times and added 30 second increments to each copy. This is the result:

st1binned2$bin <- cut(st1binned2$T, rep(1:6 * 30), include.lowest=T)
NT <- data.table(st1binned2, key="bin")
NT[, list(ang=length(unique(thetadeg)), len=length(T), Vm=mean(V), T=paste(range(T), collapse="-")), 
         by=c("A", "bin")]
#     A       bin ang len     Vm       T
# 1: 25   [30,60]   5   5 5.6772   56-60
# 2: 25   (60,90]   6   6 5.9760   61-90
# 3: 25  (90,120]   6   6 5.9760  91-120
# 4: 25 (120,150]   6   6 5.9760 121-150
# 5: 25 (150,180]   1   1 7.4700 151-151

And this is how I created the data:

st1binned2 <- do.call(rbind, replicate(4, st1binned, simplify=F))
st1binned2$T <- st1binned2$T + rep(0:3 * 30, each=nrow(df))

EDIT, to modify labels, an example:

bins <- rep(1:6 * 30)
st1binned2$bin <- cut(
  st1binned2$T, bins, include.lowest=T,
  labels=paste(head(bins, -1L), tail(bins, -1L), sep="-"), 
)
Sign up to request clarification or add additional context in comments.

3 Comments

Yes you were right that I want to bin my data into 30sec increments. However, I have also a variable named bin so I changed the st1binned2$bin to st1binned2$time. Since I also want the time to be a variable I edited your script to NT[, list(ang=length(unique(thetadeg)), len=length(T), Vm=mean(V), time=time), by=c("A", "bin")]. However, there is this brackets and parenthesis on time (which could also be seen on your answer). Any idea how to remove this? Thanks @BrodieG
@Kaye11, You can use the labels argument to cut to make your own labels, see update above.
I just observed one problem, the resulting data set should just have one unique A (as I have used some functions to compute some parameters but now its showing all the A, i.e. for your answer above, it was showing 5 25's). Sorry for not pointing this out earlier!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.