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, <environment>) : attempt to use zero-length variable name I want the output of the exists() function to be TRUE. In any case:> E$L[[1]]$T1A1[1] "2022-12-27 22:40:02 IST" Please help me solve this conundrum.... Thanking you, Yours sincerely, AKSHAY M KULKARNI [[alternative HTML version deleted]]
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 <akshay_e4 at hotmail.com> wrote:> 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, <environment>) : > attempt to use zero-length variable name > > I want the output of the exists() function to be TRUE. In any case: > > > E$L[[1]]$T1A1 > [1] "2022-12-27 22:40:02 IST" > > Please help me solve this conundrum.... > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
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]] <- list() E$L[[i]]$T1A1 <- Sys.time() exists("T1A1", where = E$L[[i]]) # returns: [1] TRUE> str(E$L)List of 1 $ :List of 1 ..$ T1A1: POSIXct[1:1], format: "2022-12-27 12:47:26" Sarah On Tue, Dec 27, 2022 at 12:36 PM akshay kulkarni <akshay_e4 at hotmail.com> wrote:> > 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, <environment>) : > attempt to use zero-length variable name > > I want the output of the exists() function to be TRUE. In any case: > > > E$L[[1]]$T1A1 > [1] "2022-12-27 22:40:02 IST" > > Please help me solve this conundrum.... > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Sarah Goslee (she/her) http://www.numberwright.com
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 27, 2022 at 10:36 AM akshay kulkarni <akshay_e4 at hotmail.com> wrote:> > 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, <environment>) : > attempt to use zero-length variable name > > I want the output of the exists() function to be TRUE. In any case: > > > E$L[[1]]$T1A1 > [1] "2022-12-27 22:40:02 IST" > > Please help me solve this conundrum.... > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com