Hopefully this is not a homework question. The other responses are fine, but I would suggest the simplest way to do exactly what you ask is if (!exists('r4')) r4 <- data.frame(a=0, b=0, c=0, d='x') The exists() function requires a character string for its first argument, i.e., the name of the object, not the object itself (check the help page for exists). Using "get" to get it doesn't make sense if it already exists. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 9/20/16, 12:31 PM, "R-help on behalf of Crombie, Burnette N" <r-help-bounces at r-project.org on behalf of bcrombie at utk.edu> wrote:>If a data.frame (r4) does not exist in my R environment, I would like to >create it before I move on to the next step in my script. How do I make >that happen? Here is what I want to do from a code perspective: > >if (exists(r4)) >{ >is.data.frame(get(r4)) >} >else >{ >a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d)) >} > >Thanks for your help, >B > > [[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.
Thank you for your time, Don. Exactly what I was looking for - a one-liner. Feedback from others on this post has been good to expand my knowledge, though. I'm too old for homework but have just started using R if/else, loops, and functions and trying to get the hang of them. Best wishes - B -----Original Message----- From: MacQueen, Don [mailto:macqueen1 at llnl.gov] Sent: Wednesday, September 21, 2016 11:26 AM To: Crombie, Burnette N <bcrombie at utk.edu>; r-help at r-project.org Subject: Re: [R] if/else help Hopefully this is not a homework question. The other responses are fine, but I would suggest the simplest way to do exactly what you ask is if (!exists('r4')) r4 <- data.frame(a=0, b=0, c=0, d='x') The exists() function requires a character string for its first argument, i.e., the name of the object, not the object itself (check the help page for exists). Using "get" to get it doesn't make sense if it already exists. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 9/20/16, 12:31 PM, "R-help on behalf of Crombie, Burnette N" <r-help-bounces at r-project.org on behalf of bcrombie at utk.edu> wrote:>If a data.frame (r4) does not exist in my R environment, I would like >to create it before I move on to the next step in my script. How do I >make that happen? Here is what I want to do from a code perspective: > >if (exists(r4)) >{ >is.data.frame(get(r4)) >} >else >{ >a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d)) } > >Thanks for your help, >B > > [[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.
> On Sep 21, 2016, at 8:26 AM, MacQueen, Don <macqueen1 at llnl.gov> wrote: > > Hopefully this is not a homework question. > > The other responses are fine, but I would suggest the simplest way to do > exactly what you ask is > > > if (!exists('r4')) r4 <- data.frame(a=0, b=0, c=0, d='x') > > > The exists() function requires a character string for its first argument, > i.e., the name of the object, not the object itself (check the help page > for exists).Since the implicit goal is to determine whether a data.frame by that name is in the search path one could add the requirement that the 'list'-mode be added as a requirement in the search: if ( !exists('r4', mode='list') ){ r4 <- data.frame(a=0, b=0, c=0, d='x')} That would avoid a 'false'-detection (or at least an unintended detection) for a function by that name if one `exist`-ed. It would also mask any `r4` that might have been a matrix or other atomic vector outside the local evaluation frame. -- David.> > Using "get" to get it doesn't make sense if it already exists. > > -Don > > -- > Don MacQueen > > Lawrence Livermore National Laboratory > 7000 East Ave., L-627 > Livermore, CA 94550 > 925-423-1062 > > > > > > On 9/20/16, 12:31 PM, "R-help on behalf of Crombie, Burnette N" > <r-help-bounces at r-project.org on behalf of bcrombie at utk.edu> wrote: > >> If a data.frame (r4) does not exist in my R environment, I would like to >> create it before I move on to the next step in my script. How do I make >> that happen? Here is what I want to do from a code perspective: >> >> if (exists(r4)) >> { >> is.data.frame(get(r4)) >> } >> else >> { >> a <- 0, b <- 0, c <- 0, d <- "x", r4 <- data.frame(cbind(a,b,c,d)) >> } >> >> Thanks for your help, >> B >> >> [[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. > > ______________________________________________ > 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.David Winsemius Alameda, CA, USA