rewrite script to eliminate constant object reference

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

rewrite script to eliminate constant object reference

bcrombie
I'm adding a column (region) to a data frame (devUni8), and the (region) column will be populated by a numeric code that is a function of another column (state) in that data frame.  See part of the script below.

How can I rewrite this so that I could apply the conditions to any data frame without having to keep typing its name (in this case, "devUni8$").  I've heard the attach() function is to be avoided, and I'm not good at with, while, or if statements right now.  I've tried some other things but keep getting "object not found" errors.  Assistance appreciated. Thanks.

#Create Region Variable
#########
devUni8$region=0

#Assign Northeast States
#########
devUni8$region[devUni8$state >= 11 & devUni8$state <= 23 | devUni8$state >= 51 & devUni8$state <= 55] = 1

#Assign Southeast States
#########
devUni8$region[devUni8$state >= 56 & devUni8$state <= 64 ] = 2
Reply | Threaded
Open this post in threaded view
|

Re: rewrite script to eliminate constant object reference

glsnow
If you want an easy way to change the name of the data frame for assignment
then you may want a macro.  There is an article in the R journal (
http://cran.r-project.org/doc/Rnews/Rnews_2001-3.pdf) on creating macros
and there are tools that help with creating macros in the gtools package.

Though what you are trying to do may work a little better using
`findInterval` or `merge`.


On Wed, Jun 12, 2013 at 2:36 PM, bcrombie <[hidden email]> wrote:

> I'm adding a column (region) to a data frame (devUni8), and the (region)
> column will be populated by a numeric code that is a function of another
> column (state) in that data frame.  See part of the script below.
>
> How can I rewrite this so that I could apply the conditions to any data
> frame without having to keep typing its name (in this case, "devUni8$").
> I've heard the attach() function is to be avoided, and I'm not good at
> with,
> while, or if statements right now.  I've tried some other things but keep
> getting "object not found" errors.  Assistance appreciated. Thanks.
>
> #Create Region Variable
> #########
> devUni8$region=0
>
> #Assign Northeast States
> #########
> devUni8$region[devUni8$state >= 11 & devUni8$state <= 23 | devUni8$state >=
> 51 & devUni8$state <= 55] = 1
>
> #Assign Southeast States
> #########
> devUni8$region[devUni8$state >= 56 & devUni8$state <= 64 ] = 2
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/rewrite-script-to-eliminate-constant-object-reference-tp4669393.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.
[hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
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 | Threaded
Open this post in threaded view
|

Re: rewrite script to eliminate constant object reference

jholtman
In reply to this post by bcrombie
Check out 'with' and 'within'.


Sent from my Verizon Wireless 4G LTE Smartphone

-------- Original message --------
From: bcrombie <[hidden email]>
Date: 06/12/2013  16:36  (GMT-05:00)
To: [hidden email]
Subject: [R] rewrite script to eliminate constant object reference
 
I'm adding a column (region) to a data frame (devUni8), and the (region)
column will be populated by a numeric code that is a function of another
column (state) in that data frame.  See part of the script below.

How can I rewrite this so that I could apply the conditions to any data
frame without having to keep typing its name (in this case, "devUni8$").
I've heard the attach() function is to be avoided, and I'm not good at with,
while, or if statements right now.  I've tried some other things but keep
getting "object not found" errors.  Assistance appreciated. Thanks.

#Create Region Variable
#########
devUni8$region=0

#Assign Northeast States
#########
devUni8$region[devUni8$state >= 11 & devUni8$state <= 23 | devUni8$state >=
51 & devUni8$state <= 55] = 1

#Assign Southeast States
#########
devUni8$region[devUni8$state >= 56 & devUni8$state <= 64 ] = 2



--
View this message in context: http://r.789695.n4.nabble.com/rewrite-script-to-eliminate-constant-object-reference-tp4669393.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] mailing list
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]]


______________________________________________
[hidden email] mailing list
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 | Threaded
Open this post in threaded view
|

Re: rewrite script to eliminate constant object reference

Ista Zahn
In reply to this post by bcrombie
Hi,

Here are some examples using the mtcars dataset that you can modify to work
with your data.

## Here is my first try using within.
## Somehow the elements of region that
## should be zero are converted to NA

mtcars <- within(mtcars, {
    region <- 0
    region[gear==4&carb==4] <- 1
    region[gear==3&carb==1] <- 2
})

## Here is a second try with an ugly
## hack to fix the NA problem

mtcars <- within(mtcars, {
    region <- NA
    region[gear==4&carb==4] <- 1
    region[gear==3&carb==1] <- 2
    region[is.na(region)] <- 0
})


## Here is an alternative using with
## instead of within
mtcars$region <- 0
mtcars$region[with(mtcars, gear==4&carb==4)] <- 1
mtcars$region[with(mtcars, gear==3&carb==1)] <- 2


Best,
Ista


On Wed, Jun 12, 2013 at 4:36 PM, bcrombie <[hidden email]> wrote:

> I'm adding a column (region) to a data frame (devUni8), and the (region)
> column will be populated by a numeric code that is a function of another
> column (state) in that data frame.  See part of the script below.
>
> How can I rewrite this so that I could apply the conditions to any data
> frame without having to keep typing its name (in this case, "devUni8$").
> I've heard the attach() function is to be avoided, and I'm not good at
> with,
> while, or if statements right now.  I've tried some other things but keep
> getting "object not found" errors.  Assistance appreciated. Thanks.
>
> #Create Region Variable
> #########
> devUni8$region=0
>
> #Assign Northeast States
> #########
> devUni8$region[devUni8$state >= 11 & devUni8$state <= 23 | devUni8$state >=
> 51 & devUni8$state <= 55] = 1
>
> #Assign Southeast States
> #########
> devUni8$region[devUni8$state >= 56 & devUni8$state <= 64 ] = 2
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/rewrite-script-to-eliminate-constant-object-reference-tp4669393.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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]]

______________________________________________
[hidden email] mailing list
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 | Threaded
Open this post in threaded view
|

RE: rewrite script to eliminate constant object reference

bcrombie

Thanks very much to everyone for their replies, especially Ista, who took the time to write script. Before posting the question I had tried attach() and then within or with statements and now know why those generated errors. Some of my syntax was incorrect.  Thanks for clearing that up. “Within” seems best to achieve the goal of not typing the name of the data frame over and over.  (The macro suggestion is also valuable for future use.) Burnette

 

From: Ista Zahn [via R] [mailto:ml-node+[hidden email]]
Sent: Wednesday, June 12, 2013 5:46 PM
To: Crombie, Burnette N
Subject: Re: rewrite script to eliminate constant object reference

 

Hi,

Here are some examples using the mtcars dataset that you can modify to work
with your data.

## Here is my first try using within.
## Somehow the elements of region that
## should be zero are converted to NA

mtcars <- within(mtcars, {
    region <- 0
    region[gear==4&carb==4] <- 1
    region[gear==3&carb==1] <- 2
})

## Here is a second try with an ugly
## hack to fix the NA problem

mtcars <- within(mtcars, {
    region <- NA
    region[gear==4&carb==4] <- 1
    region[gear==3&carb==1] <- 2
    region[is.na(region)] <- 0
})


## Here is an alternative using with
## instead of within
mtcars$region <- 0
mtcars$region[with(mtcars, gear==4&carb==4)] <- 1
mtcars$region[with(mtcars, gear==3&carb==1)] <- 2


Best,
Ista


On Wed, Jun 12, 2013 at 4:36 PM, bcrombie <[hidden email]> wrote:


> I'm adding a column (region) to a data frame (devUni8), and the (region)
> column will be populated by a numeric code that is a function of another
> column (state) in that data frame.  See part of the script below.
>
> How can I rewrite this so that I could apply the conditions to any data
> frame without having to keep typing its name (in this case, "devUni8$").
> I've heard the attach() function is to be avoided, and I'm not good at
> with,
> while, or if statements right now.  I've tried some other things but keep
> getting "object not found" errors.  Assistance appreciated. Thanks.
>
> #Create Region Variable
> #########
> devUni8$region=0
>
> #Assign Northeast States
> #########
> devUni8$region[devUni8$state >= 11 & devUni8$state <= 23 | devUni8$state >=
> 51 & devUni8$state <= 55] = 1
>
> #Assign Southeast States
> #########
> devUni8$region[devUni8$state >= 56 & devUni8$state <= 64 ] = 2
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/rewrite-script-to-eliminate-constant-object-reference-tp4669393.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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]]

______________________________________________
[hidden email] mailing list
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.


If you reply to this email, your message will be added to the discussion below:

http://r.789695.n4.nabble.com/rewrite-script-to-eliminate-constant-object-reference-tp4669393p4669404.html

To unsubscribe from rewrite script to eliminate constant object reference, click here.
NAML