首页 > 解决方案 > R 变量索引

问题描述

我有一个关于 R 如何处理索引的简单问题。我遇到的问题是,如果我有一个向量a,例如,一个包含 100 个数字的行向量,并且我想访问该向量中的第 7 个值,那么一个简单的方法就是只写 a[ 7] 我会得到我的价值。但是,如果我想使用一个变量来代替它(例如设置 b <- 7 然后写 a[b]),有时这会给我 a[7],但其他时候它会给我 a[ 中的值6]。请参见下面的示例:

a <- c(1:100)
b <- 7

a[7]
7

a[b]
6

如果这还不够信息,我很乐意发布我的实际代码以及我遇到此问题的上下文。谢谢!

** 编辑 **

这里有一些更多信息和让我对这个问题摸不着头脑的实际代码。

Seed <- 1

SimTime <- 65 #Simulation time in seconds
TimeStep <- .1 #Time between sim outputs

set.seed(Seed)

TotalSteps <- SimTime/TimeStep

TransitionProbabilities <- matrix(rbind(c(0.18766224,2.168453e-19,1.023408e-06,1.953125e-02,0.7928055,6.939317e-18,7.275958e-12),c(0.28125801,7.818222e-03,1.274877e-01,3.671646e-04,5.791627e-01,3.906250e-03,4.579670e-16),c(0.61551744,0.1333229e-01,0,9.765625e-04,6.103516e-05,2.501221e-01,3.231174e-27),c(0.06738377,2.563524e-02,0.000000e+00,1.734723e-18,2.501335e-01,6.255039e-01,3.134356e-02),c(0.55078275,1.253358e-01,1.220703e-04,9.818187e-91,7.105427e-15,3.237305e-01,2.884867e-05),c(0.32425383,8.288771e-08,5.690945e-14,1.220708e-04,1.562500e-02,1.253128e-01,5.346862e-01),c(0.32425383,8.288771e-08,5.690945e-14,1.220708e-04,1.562500e-02,1.253128e-01,5.346862e-01)),nrow = 7,ncol=7)
SlopeMeanVar <- matrix(rbind(c(5.9572805,0.8660917),c(7.9495652,5.5765029),c(2.7789705,1.1488065),c(-0.1938019,1.8259470),c(-13.3744125,83.1549576),c(-127.0796007,8048.6051287),c(-69.1002577,118.1835389)),nrow = 7,ncol=2)
TimeMeanVar <- matrix(rbind(c(36.444228,326.1963044),c(10.170391,19.0602454),c(9.890028,15.4900072),c(2.269258,3.9720946),c(1.647609,1.1667697),c(1.033581,0.4013006),c(6.512817,24.2769279)),nrow = 7,ncol=2)

TotalTime <- 0
LoopCount <- 0
LastHeight <- 0

TrueVertexList <- c()
SlopeList <- c()
TimeList <- c()
PhaseList <- c()

TrendLength <- rep(0,TotalSteps*2)
TimeColumn <- seq(0,SimTime*2-TimeStep,.1)

StartIndicies <- c()
EndIndicies <- c()

while(TotalTime < SimTime){
  LoopCount <- LoopCount + 1
  if(LoopCount == 1){
    #Choose first phase
    Phase <- sample(c(1:7),1)
    StartTime <- 0.1
    StartIndex <- 2
  }else{
    #Choose next phase using transition probabilities based on LastPhase
    Phase <- sample(c(1:7),1,prob = TransitionProbabilities[LastPhase,])
    StartTime <- TotalTime + TimeStep
    StartIndex <- StartTime*10+1
  }

  slope <- rnorm(1, mean = SlopeMeanVar[Phase,1], sd = sqrt(SlopeMeanVar[Phase,2]))
  time <- round(rnorm(1, mean = TimeMeanVar[Phase,1], sd = sqrt(TimeMeanVar[Phase,2])),digits = 1)

  EndTime <- StartTime + time - TimeStep
  EndIndex <- (EndTime) * 10 + 1

  TempTimeColumn <- seq(TimeStep,time,TimeStep)

  TrendLength[StartIndex:EndIndex] <- TempTimeColumn*slope + LastHeight

  TotalTime <- TotalTime + time
  LastHeight <- TrendLength[EndIndex]
  LastPhase <- Phase

  SlopeList <- c(SlopeList,slope)
  TrueVertexList <- c(TrueVertexList,TotalTime*10+1)
  TimeList <- c(TimeList, time)
  PhaseList <- c(PhaseList, Phase)

  StartIndicies <- c(StartIndicies, StartIndex)
  EndIndicies <- c(EndIndicies, EndIndex)
  plot(TimeColumn[1:TotalTime*10+1],TrendLength[1:TotalTime*10+1],type='l')
}

> StartIndex
[1] 650
> TrendLength[StartIndex]
[1] 341.1459
> TrendLength[650]
[1] 337.7889
> TrendLength[649]
[1] 341.1459

感谢大家的任何指导。

标签: r

解决方案


所以问题是

StartIndex == 650
# [1] FALSE

您的StartIndex变量实际上不是 650。如果您这样做,那就更清楚了

print(StartIndex, digits=20)
# [1] 649.99999999999989

使用浮点数从数组中提取值时需要格外小心。你想自己round()truncated()他们自己,所以你知道到底发生了什么。确保您的索引始终是整数。


推荐阅读