[R] Is there a facility in R similar to MatLab syms that allows using unevaluated numeric symbols in matrices?

2007-07-18 Thread Dylan Arena
Hi,


I'm trying to use R to get eigenvalues and eigenvectors of a matrix 
whose elements are of the form (2 * lambda), -(lambda + mu), etc.  I'd 
like R to treat this matrix as a numeric matrix without treating lambda 
and mu as variable names but rather as some sort of atomic quantities 
(and hence give eigenvectors in terms of mu and/or lambda).  MatLab and 
Mathematica both do this, but I'm not sure whether R does.  Does anyone 
have any ideas about how to do this?


Please let me know,
Dylan

__
R-help@stat.math.ethz.ch 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.


[R] Is there a quick way to count the number of times each element in a vector appears?

2007-03-05 Thread Dylan Arena
Hi there,


I'm writing a function that calculates the probability of different
outcomes of dice rolls (e.g., the sum of the highest three rolls of
five six-sided dice).  I'm using the combinations function from the
gtools package, which is great: it gives me a matrix with all of the
possible combinations (with repetitions allowed).  Now I want to count
the number of times each element appears in each arrangement so I can
calculate the number of permutations of that arrangement.  E.g., if I
get output like:

 combinations(3,3, rep=TRUE)
  [,1] [,2] [,3]
 [1,]111
 [2,]112
 [3,]113
 [4,]122
 [5,]123
 [6,]133
 [7,]222
 [8,]223
 [9,]233
[10,]333

I'd like to be able to determine that the first row has 3 repetitions,
yielding 3!/3! = 1 permutation, while the second row has 3
repetitions, yielding 3!/2! = 3 permutations, etc.  (This gets harder
when there are large numbers of dice with many faces.)

I know there are simple things to do, like iterating over the rows
with for loops, but I've heard that for loops are sub-optimal in R,
and I'd like to see what an elegant solution would look like.

E.g., I might like to use sapply() with whatever function I come up
with; I thought of using something like duplicated() and just counting
the number of TRUEs that are returned for each vector (since the
elements are always returned in non-decreasing order), but I'm
optimistic that there is a better (faster/cleaner) way.

So here is my question in a nutshell:
Does anyone have ideas for how I might efficiently process a matrix
like that returned by a call to combinations(n, r, rep=TRUE) to
determine the number of repetitions of each element in each row of the
matrix?  If so, I'd love to hear them!


Thanks very much for your time,
Dylan Arena
(Statistics M.S. student)

__
R-help@stat.math.ethz.ch 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.