【发布时间】:2014-06-14 00:02:15
【问题描述】:
我正在尝试编写一个代码来标识 R 中重复时间序列的长度(以秒为单位),并将每个序列子集到它自己的数据帧中以进行曲线拟合和分析。每个序列都是传感器电压输出的时间序列,必须单独分析。
我的代码看起来很笨重,但它可以像这里写的那样工作。我试图弄清楚是否有一个包或简单的步骤我错过了更优雅地执行此操作。秒数是十进制秒数,数据可以是数字或整数,这对于本示例无关紧要。这不是实际的传感器输出,而是相同的格式。
set.seed(1)
all_data = data.frame( sec = rep(1.8:4,9), data = sample(1:27), data2 = sample(5:7))
#identify time step length in seconds
lowest = min(all_data$sec)
highest = max(all_data$sec)
#put into data frame
time_step = c(lowest,highest)
#find index of first time period
matches = match(time_step,all_data[,1])
#subset first time period
total_measures = nrow(all_data)/matches[2]
all_data = all_data[matches[1]:nrow(all_data),]
# test_frame = data.frame(c(1,2))
n = matches[2]
#counter for number of measures in file
count = c(1:(nrow(all_data)/n))
count2 = c(0:(nrow(all_data)/n-1))
# subset to break each measure into its own workable file
eq = paste("subd",count," = all_data[((",count2,"*n)+1):(",count,"*n),]",sep = "")
eval(parse(text = eq))
谢谢!
【问题讨论】:
标签: r loops time-series subset