错误:尝试分配给数组时没有“dimnames"属性 [英] Error: no 'dimnames' attribute when trying to assign to array

查看:37
本文介绍了错误:尝试分配给数组时没有“dimnames"属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

`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)
}

这是一个模拟马尔可夫链的函数,但是每当我运行它时,我都会收到错误,数组没有dimnames"属性,知道为什么会发生这种情况

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

推荐答案

你的数组 'mysequence' 是字符,所以 R 试图找到 rowname 匹配 mysequence[i-1] 的行.如果您没有在 'trans' 上设置行名,这将引发您的错误.要么使用整数值从 trans 中选择一行,要么在 trans 上设置行名,具体取决于您实际尝试执行的操作.

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.

这篇关于错误:尝试分配给数组时没有“dimnames"属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆