Remember how we typically index elements of a list with the double brackets
[[]].
So foo(aye,bee)[[1]] will return your matrix.
Now combine that with our knowledge of how to subset matrices.
foo(aye,bee)[[1]][1,1]
is what you want.
On Wed, Feb 11, 2009 at 2:46 PM, John-Paul Ferguson <jpferg at mit.edu> wrote:
(Apologies if this arrives twice. I first sent it from
a gmail account.)
Hello All,
Another noob question. Assume I have function foo():
foo <- function(a,b) {
*does some stuff...*
*creates a matrix J
creates a scalar k
*list(J,k)
}
If you run this function, then foo(aye,bee) will return something like
this:
[[1]]
[,1] [,2]
[1,] .5 .2
[2,] .4 .3
[[2]]
[,1]
[1,] .8
Let us assume that you want to reference the [1,1]th element of J. If the
function ONLY returned that matrix, then the syntax is the following:
bar <- foo(aye,bee)[1,1]
So far, so good. I cannot figure out how to call that same element when the
function returns two things, though. None of the following work:
baz <- foo(aye,bee)[1,1]
baz <- J.foo(aye,bee)[1,1]
baz <- foo.J(aye,bee)[1,1]
baz <- foo(aye,bee)[1[1,1]]
baz <- foo(aye,bee)[1,1,1]
baz <- foo(aye,bee)[J:1,1]
Does anyone know what the RIGHT syntax is?
Best,
John-Paul
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l
--
Patrick Lam
Department of Government and Institute for Quantitative Social Science,
Harvard University