我有一个听起来容易的问题,但我真的找不到错误。我有3377个数据点(体温测量值)。采样率为5分钟,我想将数据放入矩阵中。但是,一旦将所有3377个数据点放入矩阵,R就开始回收。为了防止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
}
}
}
但是,循环不会停止...它会循环到矩阵中的最后一个单元格。根据我的计算,最后79个细胞应保持自由(即NA,因为3456个细胞--3377 = 79),但这只适用于过去8个左右......
任何提示错误的提示?
谢谢!
答案 0 :(得分:3)
我认为这可以做你想做的事情:
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)
答案 1 :(得分:-2)
好的,尝试一个例子,它会告诉你你的错误在哪里...... 叹息。循环必须是:
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
}
}
}
非常感谢!
最佳, 恭