[R] "non-numeric argument to binary operator"

Peter Langfelder peter.langfelder at gmail.com
Wed Jun 15 01:51:08 CEST 2011


Not sure ho to run the code since nn is not defined.

Most likely reason is that the number of unique values in the rows of
habs is not the same. That makes the tables produced in the apply
command of different length and apply returns a list instead of a
matrix, which makes the t() or the division choke since a list (as
opposed to a matrix) is not considered a numeric type.

Try printing apply(habs, 1, table) when you get the error.

HTH,

Peter

On Tue, Jun 14, 2011 at 4:31 PM, the_big_kowalski <bkowalski at csumb.edu> wrote:
> Hello,
> For reasons unclear to me I am getting above error message when I change the
> value of b in the following code:
>
>    rm(list=ls())
>    library("Hmisc")
>    set.seed(4) #seems to have an effect on t(apply
>    b=10
>    pop.props = c(0.6,0.2,0.2)
>    x=matrix(pop.props,nrow=nn,ncol=length(pop.props), byrow=T)
>    print(x)
>    habs = rMultinom((x),b)
>    print(habs)
>    habs=t(apply(habs, 1, table))/b  # works for b=10, but not for b=5???
>
> Also when i run this code in the iteration loop, it seems that changing seed
> value also has an effect on whether I get the code to run in entirety, or if
> it chokes with the above error message.
>
> I thank you in advance for your help,
>
>



More information about the R-help mailing list