If you write your code as functions you can avoid the nasty
'if(exists("x"))x<-...' business this by writing default values for
arguments to your function.   They will be computed only when
they are used.  E.g.,

analyzeData <- function(a=0, b=0, c=0, d="x", r4 = data.frame(a, b, c, d)) {
    summary(r4)
}

> analyzeData(c=101:102)
       a           b           c         d
 Min.   :0   Min.   :0   Min.   :101.0   x:2
 1st Qu.:0   1st Qu.:0   1st Qu.:101.2
 Median :0   Median :0   Median :101.5
 Mean   :0   Mean   :0   Mean   :101.5
 3rd Qu.:0   3rd Qu.:0   3rd Qu.:101.8
 Max.   :0   Max.   :0   Max.   :102.0
> analyzeData(r4=data.frame(a=10:11,b=20:21,c=30:31,d=c("x","y")))
       a               b               c         d
 Min.   :10.00   Min.   :20.00   Min.   :30.00   x:1
 1st Qu.:10.25   1st Qu.:20.25   1st Qu.:30.25   y:1
 Median :10.50   Median :20.50   Median :30.50
 Mean   :10.50   Mean   :20.50   Mean   :30.50
 3rd Qu.:10.75   3rd Qu.:20.75   3rd Qu.:30.75
 Max.   :11.00   Max.   :21.00   Max.   :31.00



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Sep 20, 2016 at 12:31 PM, Crombie, Burnette N <bcrom...@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@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]]

______________________________________________
R-help@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.

Reply via email to