Re: [R] if/else help

2016-09-22 Thread Crombie, Burnette N
Thanks very much for your detailed reply to my post.  Very helpful/useful 
tool(s) you’ve provide me.  Best wishes, B.

From: William Dunlap [mailto:wdun...@tibco.com]
Sent: Wednesday, September 21, 2016 10:48 AM
To: Crombie, Burnette N <bcrom...@utk.edu>
Cc: r-help@r-project.org
Subject: Re: [R] if/else help

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<http://tibco.com>

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

Re: [R] if/else help

2016-09-22 Thread Crombie, Burnette N
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:macque...@llnl.gov] 
Sent: Wednesday, September 21, 2016 11:26 AM
To: Crombie, Burnette N <bcrom...@utk.edu>; r-help@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-boun...@r-project.org on behalf of 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.

__
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.


Re: [R] if/else help

2016-09-21 Thread David Winsemius

> On Sep 21, 2016, at 8:26 AM, MacQueen, Don  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"
>  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.
> 
> __
> 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.

David Winsemius
Alameda, CA, USA

__
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.


Re: [R] if/else help

2016-09-21 Thread MacQueen, Don
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"
 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.

__
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.


Re: [R] if/else help

2016-09-21 Thread William Dunlap via R-help
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 
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.


Re: [R] if/else help

2016-09-21 Thread David Winsemius

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

Please, please, please, do not encourage people to use the construction:

data.frame(cbind(

...and learn to distrust whatever misguided example you learned it from. (Yes, 
I know there is one in the help pages but that is an exception to the rule.)

There is no reason to use it. It would be much clearer to do this:

r4 <- data.frame(a = 0, b = 0, c = 0, d = "x")# only one column is 
factor-class.

If you did it your way you would have gotten four columns of factors, (first 
cbind() creates a character matrix, and then data.frame() creates factors)  ... 
which does not appear to be what you expected.

And ... as always has been the case on Rhelp, ... learn to post in plain text.

> 
> __
> 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.

David Winsemius
Alameda, CA, USA

__
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.


Re: [R] if/else help

2016-09-21 Thread Jeff Newmiller
Get rid of the commas? Get rid of the get() function call? Get rid of the 
cbind() function call?  Post using plain text format so the HTML doesn't screw 
up code? Read the Posting Guide? All of these ideas have merit IMHO...
-- 
Sent from my phone. Please excuse my brevity.

On September 20, 2016 12:31:47 PM PDT, "Crombie, Burnette N"  
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.

__
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.


[R] if/else help

2016-09-21 Thread Crombie, Burnette N
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.