Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用循环函数_R_Loops_Dataset - Fatal编程技术网

R 使用循环函数

R 使用循环函数,r,loops,dataset,R,Loops,Dataset,我有一个数据集,我想根据变量范围(时间)重复计算一些参数。因此,我的数据集如下所示: 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.

我有一个数据集,我想根据变量范围(时间)重复计算一些参数。因此,我的数据集如下所示:

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")
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>)
这段代码非常适合计算我的参数:

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")]

NT我想您希望以30秒为增量对数据进行装箱。如果这是您想要的,您可以使用
cut
。为了演示,我必须创建一些间隔超过30秒的数据。我只是复制了你的数据帧三次,每次增加30秒。结果是:

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

st1binned2$bin查看
cut()
s帮助。我从未使用过
data.table
,所以我不知道它是否有用。是的,你说得对,我想以30秒为增量存储数据。但是,我还有一个名为bin的变量,因此我将
st1binned2$bin
更改为
st1binned2$time
。因为我也希望时间是一个变量,所以我将您的脚本编辑为
NT[,list(ang=length(unique(thetadeg)),len=length(T),Vm=mean(V),time=time),by=c(“a”,“bin”)]
。然而,在时间上有括号和括号(这也可以在你的答案上看到)。你知道怎么去掉这个吗?谢谢@BrodieG@Kaye11,您可以使用
labels
参数来
cut
制作自己的标签,请参见上面的更新。我刚刚观察到一个问题,生成的数据集应该只有一个唯一的A(因为我使用了一些函数来计算一些参数,但现在它显示了所有的A,也就是说,对于上面的答案,它显示了5个25)。很抱歉没有在前面指出这一点!
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
st1binned2 <- do.call(rbind, replicate(4, st1binned, simplify=F))
st1binned2$T <- st1binned2$T + rep(0:3 * 30, each=nrow(df))
bins <- rep(1:6 * 30)
st1binned2$bin <- cut(
  st1binned2$T, bins, include.lowest=T,
  labels=paste(head(bins, -1L), tail(bins, -1L), sep="-"), 
)