【发布时间】:2018-01-24 15:26:58
【问题描述】:
编写 R 代码,用火车上 wi-fi 系统上接收到的数据量填充 2 个向量。
(最终用接收到的数据向量中的数据填充空向量)
movingnorth <- vector('numeric')
movingsouth <- vector('numeric')
(每个向量包含 2800 个值) 纬度
(如果火车向北移动,则用接收到的向量中的数据填充移动的北向量)
for(y in 1:length(latitude)){
if (y < length(latitude)){
if(latitude[y]<= latitude[y+1]){
movingnorth <- c(movingnorth, received[y])
}
else {
break()
}
}
}
(如果火车向南移动,则用接收到的向量中的数据填充向南移动的向量)
for(z in 1:length(latitude)){
if (z < length(latitude)){
if(latitude[z] >= latitude[z+1]){
movingsouth <- c(movingsouth, received[z])
}
else {
break()
}
}
}
问题在于movingnorth 和movingsouth 向量填充了相同的数据。运行代码后,北移向量包含 632 个值,南移包含 109 个,但是这 109 个值与北移的前 109 个值匹配。显然,火车不可能同时向北和向南移动。
【问题讨论】:
-
您可以添加示例数据和所需的输出吗?
-
也许对于前 109 辆列车,火车静止不动,因此满足
latitude[y] == latitude[y+1]的条件?这对latitude[y] <= latitude[y+1]和latitude[z] >= latitude[z+1]都有效。或者它正好向西或向东移动,并且没有记录到南北移动,因此没有改变纬度。 -
@LAP 你说得对,前 109 趟火车还在。