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

Peter Langfelder peter.langfelder at gmail.com
Wed Jun 15 19:25:05 CEST 2011


Write yourself an alternative function to table, for example like this:

tableOfGivenLevels = function(x, levels)
{
  n = length(levels)
  counts = rep(0, n);
  names(counts) = levels
  tab = table(x);
  counts[match(names(tab), levels)] = tab;
  counts;
}

x = sample(c(1:4), 20, replace = TRUE)
tableOfGivenLevels(x, c(0:6))

Then run your old code, the one with

 habs=t(apply(habs, 1, table))/b

but replace table by tableOfGivenLevels.

Peter


On Tue, Jun 14, 2011 at 10:43 PM, the_big_kowalski <bkowalski at csumb.edu> wrote:
> "Most likely reason is that the number of unique values in the rows of
> habs is not the same."
>
> Yes, I think that is the problem, thank you.
> How would I write the code to include 0s in the matrix,
> ie, I want to have a record of when 1, 2, or 3 does not get sampled,
> to come up with a frequency of each value for each nn (which in this case is
> 5)
>
> I tried 'factor' but what ends up happening is that the matrix gets reduced
> to zero
> after a couple of iterations.
>
> habs=t(apply(habs, 1, function(x) table(factor(x, levels = 1:3))))
>
> --
> View this message in context: http://r.789695.n4.nabble.com/non-numeric-argument-to-binary-operator-tp3598160p3598563.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Sent from my Linux computer. Way better than iPad :)



More information about the R-help mailing list