【问题标题】:R: For loops with different criteria filling 2 vectors with the same dataR:对于具有不同标准的循环,用相同的数据填充 2 个向量
【发布时间】: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] &lt;= latitude[y+1]latitude[z] &gt;= latitude[z+1] 都有效。或者它正好向西或向东移动,并且没有记录到南北移动,因此没有改变纬度。
  • @LAP 你说得对,前 109 趟火车还在。

标签: r for-loop vector


【解决方案1】:

你不需要循环,让我们看看这个玩具示例:

latitude <- c(1,2,1,3,2) # latitude coordinates
received <- 1:5

require(data.table)
d <- data.table(lat = latitude, rec = received)
d
#    lat rec
# 1:   1   1
# 2:   2   2
# 3:   1   3
# 4:   3   4
# 5:   2   5
d[, dif := c(0, diff(lat))] # if positive moved N, if negative S
movingnorth <- d[dif >= 0, rec]
movingsouth <- d[dif < 0, rec]
movingnorth
#[1] 1 2 4
movingsouth
# [1] 3 5

【讨论】:

  • 当您在表“dif”中创建新列时,为什么使用 c(0, diff(lat)) 而不仅仅是 diff(lat)? @minem
  • @Brad 因为 diff(lat) 不计算第一个元素和第一个元素之间的差异(即 0),所以我手动添加它,所以我们可以获得正确的长度向量。
【解决方案2】:

您不应该为此使用循环。它可以通过矢量化的方式轻松完成:

#reproducible example
set.seed(42)
latitude <- sample(20, replace = TRUE)
#[1] 19 19  6 17 13 11 15  3 14 15 10 15 19  6 10 19 20  3 10 12
received <- rnorm(20)
#[1]  1.3048697  2.2866454 -1.3888607 -0.2787888 -0.1333213  0.6359504 -0.2842529 -2.6564554 -2.4404669  1.3201133
#[11] -0.3066386 -1.7813084 -0.1719174  1.2146747  1.8951935 -0.4304691 -0.2572694 -1.7631631  0.4600974 -0.639994

#find directions, be a bit fuzzy around zero, adjust for precision of latitudes if necessary
direction <- cut(diff(latitude), c(-Inf, -1e-16, 1e-16, Inf), c("south", "neither", "north")) 
#[1] neither south   north   south   south   north   south   north   north   south   north   north   south   north   north  
#[16] north   south   north   north  
#Levels: south neither north

#split the vector (after discarding last value)
split(head(received, -1), direction)
#$south
#[1]  2.2866454 -0.2787888 -0.1333213 -0.2842529  1.3201133 -0.1719174 -0.2572694
#
#$neither
#[1] 1.30487
#
#$north
#[1] -1.3888607  0.6359504 -2.6564554 -2.4404669 -0.3066386 -1.7813084  1.2146747  1.8951935 -0.4304691 -1.7631631
#[11]  0.4600974

#if neither needs to be combined with north
head(received, -1)[direction %in% c("neither", "north")]
#[1]  1.3048697 -1.3888607  0.6359504 -2.6564554 -2.4404669 -0.3066386 -1.7813084  1.2146747  1.8951935 -0.4304691
#[11] -1.7631631  0.4600974

【讨论】:

    猜你喜欢
    • 2015-10-08
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多