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 - Fatal编程技术网

R循环不停止

R循环不停止,r,loops,R,Loops,我有一个听起来很容易的问题,但我真的找不到错误。我有3377个数据点(体温测量)。采样率为5分钟,我想将数据放入矩阵中。然而,R一旦将所有3377个数据点放入矩阵中,就开始循环使用。为了防止r这样做,我写了一个循环,我希望循环在向量结束时停止 Ankle.r <- 1:3377 # Example data a = 288 # sampling rate = 5min -> 288 measurement points per day c = 11 # 11 full days

我有一个听起来很容易的问题,但我真的找不到错误。我有3377个数据点(体温测量)。采样率为5分钟,我想将数据放入矩阵中。然而,R一旦将所有3377个数据点放入矩阵中,就开始循环使用。为了防止r这样做,我写了一个循环,我希望循环在向量结束时停止

 Ankle.r <- 1:3377 # Example data
 a = 288 # sampling rate = 5min -> 288 measurement points per day
 c = 11 # 11 full days of sampling (and a few more points, wherefore the matrix is to be 12 rows)

Ankle.r2 <- matrix(NA, ncol = a, nrow = c+1) # matrix with NAs for 12 days with 288 cols each (=3456 cells)
x <- length (Ankle.r) # total number of data points, is 3377
for (f in 1:(c+1)){   # for each row
  for (p in 1:a){     # for each column (i.e. cell)
    st_op <- (((f-1)*p)+p)  # STOP criterion, gives the number of cells that have already been filled
    if (st_op<x){     # only perform operation if the number of cells filled is < the number of data points (i.e. 3377)
      Ankle.r2[f,p] <- Ankle.r[(((f-1)*p)+p)]
    } else {stop
    }
  }
}
Ankle.r每天288个测量点
c=11#11整天的采样(还有几个点,因此矩阵为12行)

Ankle.r2我想这就是你想要做的:

Ankle.r <- 1:3377 # Example data
a = 288 # sampling rate = 5min -> 288 measurement points per day
c = 11 

length(Ankle.r) <- a * (c + 1) #pad input vector with NA values
m <- matrix(Ankle.r, ncol = a, byrow = TRUE)
Ankle.r每天288个测量点
c=11

长度(Ankle.r)好的,试试一个例子,它会告诉你你的错误在哪里……叹气。循环必须是:

 Ankle.r2 <- matrix(NA, ncol = a, nrow = c+1) # matrix with NAs for 12 days with 288 cols each (=3456 cells)
 x <- length (Ankle.r) # total number of data points, is 3377
 for (f in 1:(c+1)){   # for each row
   for (p in 1:a){     # for each column (i.e. cell)
     st_op <- (((f-1)*a)+p)  # STOP criterion, gives the number of cells that have already been filled
     if (st_op<=x){     # only perform operation if the number of cells filled is < the number of data points (i.e. 3377)
       Ankle.r2[f,p] <- Ankle.r[(((f-1)*a)+p)]
     } else {stop
     }
   }
 }

Ankle.r2
stop()
是一个产生错误的函数。如果你想叫它,你需要括号。我认为您正在寻找的是
中断
,就像您试图使用
停止
一样。也就是说,这是一个非常黑客的解决方法。寻找一种不涉及for循环和停止条件的方法。嗯……这可能是真的。不幸的是,我发现很难想出一个不同的想法。你有什么建议吗?谢谢当然试试罗兰的答案。