Re: [R] extract from a list of lists

2022-12-27 Thread Bill Dunlap
I find your original sapply(List, function(element)element$name) easy to understand. However replacing sapply with vapply makes for more robust code. vapply requires you to supply the expected mode (type) and length of element$name and if any of the elements don't comply with that, vapply gives

Re: [R] extract from a list of lists

2022-12-27 Thread Therneau, Terry M., Ph.D. via R-help
Thanks everyone for prodding my gray matter, which seems to be getting stiffer as I approach 70 (< 90 days).  --  I'll continue to use the $ or [[ forms.   That will suffice. --  I thought there might be a base R variant, e.g. something like  extract( list, element-name); probably cross

Re: [R] extract from a list of lists

2022-12-27 Thread Bert Gunter
Well, I prefer Greg's approach, but if you want to avoid calls to $ or `[[` then you could do: unlist(fits)[ rep(names(fits[[1]]) == 'iter', length(fits))] Cheers, Bert On Tue, Dec 27, 2022 at 9:46 AM Greg Snow <538...@gmail.com> wrote: > > Another option is the map family of functions in the

[R] MCMC tools

2022-12-27 Thread Therneau, Terry M., Ph.D. via R-help
Is there a convenient package that computes standard covergence summaries for and MCMC run?    This is something that I likely knew once and have now forgotton.  More detail:  I'm trying to understand the MCMC done by a particular model called Subtype and Stage Inference (SuStaIn), suffice

Re: [R] error in exists.....

2022-12-27 Thread Bert Gunter
There is no substitute for experience/knowledge; nor any universally optimal strategy for debugging. But using the debugging tools that R provides -- ?debug, ?traceback, ?recover (e.g. in the form of options(error = utils::recover) ) -- should always be the first goto (or maybe the second after

Re: [R] error in exists.....

2022-12-27 Thread akshay kulkarni
Dear Bert, Yeah, it's workingSometimes (only sometimes) I feel that I am troubling this list with very simple questions the answer to which I always happen to find myself a little after...any solutions for that (you seem to be an omniscient person)!? Thanking you, Yours

Re: [R] error in exists.....

2022-12-27 Thread Bert Gunter
You are confused about the list hierarchy. Perhaps this will explain: > i <- 1 > E <- new.env() > E$L <- list() ## L is an empty list in E > i <- 1 >E$L[[i]]$T1A1 <- Sys.time() Error in `*tmp*`[[i]] : subscript out of bounds ## L is empty, so L[[1]] does not exist; is not a list; ##

Re: [R] error in exists.....

2022-12-27 Thread Greg Snow
You might try `hasName` instead of `exists` since `exists` is designed for environments and `hasName` for objects (like lists). Note that the order of the arguments is switched between the 2 functions. This does the same thing as Andrew Simmons' answer, but is a little bit shorter. On Tue, Dec

Re: [R] error in exists.....

2022-12-27 Thread akshay kulkarni
Dear Sarah, I had this also before the assignment of Sys.time(): > E$L[[i]] <- i After assignment of Sys.time(): > E$L [[1]] [[1]][[1]] [1] 1 [[1]]$T1A1 [1] "2022-12-27 22:40:02 IST" regrets for not sharing this ..Can you reproduce it now? Thanking you, Yours

Re: [R] error in exists.....

2022-12-27 Thread Sarah Goslee
Hi, I can't create the desired object using the code you provided, but if I create it in two steps so that E$L[[i]]$T1A1 does exist, exist() returns TRUE. E <- new.env() E$L <- list() i <- 1 E$L[[i]]$T1A1 <- Sys.time() # returns: Error in `*tmp*`[[i]] : subscript out of bounds E$L[[i]] <-

Re: [R] error in exists.....

2022-12-27 Thread Andrew Simmons
exists() is for bindings in an environment, not for names in a list. try "T1A1" %in% names(E$L[[i]]) instead On Tue, Dec 27, 2022, 12:36 akshay kulkarni wrote: > Dear members, > I have the following code: > > E <- new.env() > > E$L <- list() > > i <- 1 > >

Re: [R] extract from a list of lists

2022-12-27 Thread Greg Snow
Another option is the map family of functions in the purrr package (yes, this depends on another package being loaded, which may affect things if you are including this in your own package, creating a dependency). In map and friends, if the "function" is a string or integer, then it is taken as

[R] error in exists.....

2022-12-27 Thread akshay kulkarni
Dear members, I have the following code: > E <- new.env() > E$L <- list() > i <- 1 > E$L[[i]]$T1A1 <- Sys.time() > exists("T1A1", where = E$L[[i]]) Error in list2env(list(1, T1A1 = 1672161002.38743), NULL, ) : attempt to use zero-length variable name I want the

Re: [R] extract from a list of lists

2022-12-27 Thread Greg Snow
Terry, I don't know if it is much cleaner or not, but you can use: sapply(fits, `[[`, 'iter') This calls the `[[` function (to extract list elements) on each element of the top list, with the extra argument of `iter` to say which element. On Tue, Dec 27, 2022 at 10:16 AM Therneau, Terry M.,

[R] extract from a list of lists

2022-12-27 Thread Therneau, Terry M., Ph.D. via R-help
I not uncommonly have the following paradym    fits <- lapply(argument, function) resulting in a list of function results.   Often, the outer call is to mclapply, and the function encodes some long calculation, e.g. multiple chains in an MCMC. Assume for illustration that each function returns