Hi, I wrote the function which outputs a matrix 'c' and a single value 'd', as follows (simplified example): procedure <- function(a,b){ ... list(c,d) } now I want to use 'c' and 'd' in code as follows: d <- matrix(0,1,1) value <- procedure(a,b) and d[1,1] <- value[2] breaks telling that: Error in d[1, 1] : incorrect number of dimensions What I did wrong??, best, robert -- View this message in context: http://www.nabble.com/function-return-output-tp21496413p21496413.html Sent from the R help mailing list archive at Nabble.com.
threshold wrote:> Hi, I wrote the function which outputs a matrix 'c' and a single value 'd', > as follows (simplified example): > procedure <- function(a,b){ > ... > list(c,d) > } > now I want to use 'c' and 'd' in code as follows: > d <- matrix(0,1,1) > value <- procedure(a,b) > and d[1,1] <- value[2] breaks telling that: > Error in d[1, 1] : incorrect number of dimensions > What I did wrong??, best, robertProbably "value" is a list, hence value[2] is also a list and you cannot assign a list to an element of a *numeric* matrix. I guess you want value[[2]]. Anyway, it is all a guess since we do not know what your "procedure()" returns .... Uwe Ligges> > >
On Jan 16, 2009, at 5:22 AM, threshold wrote:> > Hi, I wrote the function which outputs a matrix 'c' and a single > value 'd', > as follows (simplified example): > procedure <- function(a,b){ > ... > list(c,d) > } > now I want to use 'c' and 'd' in code as follows: > d <- matrix(0,1,1) > value <- procedure(a,b) > and d[1,1] <- value[2] breaks telling that: > Error in d[1, 1] : incorrect number of dimensions > What I did wrong??, best, robertWho knows? No reproducible code. The usual way of accessing just the second element of a list would be value[[2]]. value[2] will be a list that contains whatever was "d", rather than what was "d" in the procedure() call itself. It will not have the name "d". It is possible to use the same names inside and outside a function but it may be more clear for beginners to keep them separate. -- David Winsemius