Error: no 'dimnames' attribute when trying to assign to array

久未见 提交于 2019-12-24 12:19:07

问题


`function(trans,initprob,N)'
{
  BrokerPosition <- c("BP", "IP", "SP")
  mysequence <- character()
  firstposition <- sample(BrokerPosition, 1, rep=TRUE, prob=initprob)
  mysequence[1]   <- firstposition

  for (i in 2:N) {
    prevposition <- mysequence[i-1]
    probabilities <- trans[prevposition,]
    BPosition <- sample(BrokerPosition, 1, rep=TRUE, prob=probabilities)
    mysequence[i] <- BPosition
  }

return(mysequence)
}

This is a function made to simulate Markov chain , but whenever I run it I get the error no 'dimnames' attribute for array ,any idea why is this happening


回答1:


Your array 'mysequence' is character, so R is trying to find the row with rowname matching mysequence[i-1]. If you don't set rownames on 'trans', this will throw your error. Either use a integer value to select a row from trans, or set the rownames on trans, depending on what you are actually trying to do.



来源:https://stackoverflow.com/questions/16243051/error-no-dimnames-attribute-when-trying-to-assign-to-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!