+ Reply to Thread
Results 1 to 8 of 8

Thread: create or rename variables with assign

  1. #1
    Points: 136, Level: 2
    Level completed: 72%, Points required for next Level: 14

    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    create or rename variables with assign



    Hi, I asked a question similar to this a few days ago, but I'd still like to figure out a way to do the following (even if this is not the most efficient way, I have other uses for learning the method)

    Suppose I have n variables, index1,index2...indexn, and would like to rename them r1,r2...rn using assign...so far I have something like this

    Code: 
    > for (i in 1:n){
    + nam=paste("r",i,sep="")
    + assign(nam,paste("index",i,sep=""))
    + }

    This won't work, because if I type e.g., r2, it spits out "index2" but I'd like it to spit out the numeric output of what index2 corresponds to. I've tried things like "get" and "noquote" but I'm doing something that is probably simple wrong.

    In other words, this is what I get:

    Code: 
    > r2
    [1] "index2"
    This is what I want

    Code: 
    > index2=c(5400,5401,5402)  ## Suppose for example this is index 2,
    
    > r2
    5400 5401 5402
    I'd also like to adapt any solution to things like r2=index2[5:7], r3=index3[5:7], etc...
    Last edited by cmc0605; 10-19-2012 at 04:36 PM.

  2. #2
    TS Contributor
    Points: 6,533, Level: 52
    Level completed: 92%, Points required for next Level: 17
    Lazar's Avatar
    Location
    Sydney
    Posts
    660
    Thanks
    109
    Thanked 163 Times in 148 Posts

    Re: create or rename variables with assign

    Not sure I know what you mean by the last line but could you not get what you want by something like:

    Code: 
    x<- data.frame(index1 = 1:3, index2 = 1:3, index3 = 1:3)
    names(x)<- paste0('r', 1:3)

  3. #3
    RotParaTon
    Points: 46,091, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterActivity AwardCommunity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,061
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    Re: create or rename variables with assign

    It's a bad way to do things but if you're just interested in the method...

    Code: 
    for (i in 1:n){
      nam=paste("r",i,sep="")
      assign(nam, get(paste("index",i,sep="")))
    }
    If we think about what your code was doing before - you were just assigning to nam the character string "index1" or whatever. You need to actually get the object by the name of "index1" and you can do that using `get`.
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  4. #4
    Points: 136, Level: 2
    Level completed: 72%, Points required for next Level: 14

    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: create or rename variables with assign

    Quote Originally Posted by Dason View Post
    It's a bad way to do things but if you're just interested in the method...

    Code: 
    for (i in 1:n){
      nam=paste("r",i,sep="")
      assign(nam, get(paste("index",i,sep="")))
    }
    If we think about what your code was doing before - you were just assigning to nam the character string "index1" or whatever. You need to actually get the object by the name of "index1" and you can do that using `get`.
    Dason, thanks. Now a follow-up question...suppose that with each index vector I want to extract every 12th value, for instance corresponding to a month of the year. As an example, if I had index1, index2,index3...index12, then I'd also have a janindex1, janindex2,febindex1,febindex2,etc (there would be 144 such values). I am wondering how to modify the below code to get each variable in that situation

    Code: 
    > j=0
    +for (i in 1:2){
    + nam=paste("janindex",i,sep="")
    + assign(nam,get(paste("index",i[1:(j+12)==(j+1)],sep="")))
    + nam2=paste("febindex",i,sep="")
    + assign(nam2,get(paste("index",i[1:(j+12)==(j+2)],sep="")))
    ## etc etc, for each month
    + }
    In this case, typing "janindex1" gives me all the values for index1, when I really want every 12th value in index1. (Please note that all these indices are already defined, and have a length sufficient for extracting every 12th value). My guess is that there needs to be some other way to write the
    Code: 
    "i[1:(j+12)==(j+2)]"
    part since it's only reading it as though the "i" were there.

    As an example,

    Code: 
    > index5
     [1] 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615
    [16] 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630
    [31] 9631 9632 9633 9634 9635 9636
    then,

    Code: 
    > janindex5
    [1] 9601 9613 9625
    I'm looking for a rapid way to define all these variables if there is a large number of index values times the number of months within each index...

  5. #5
    RotParaTon
    Points: 46,091, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterActivity AwardCommunity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,061
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    Re: create or rename variables with assign

    I still don't understand why you want variables named "janindex" and "febindex" instead of having a master list so that mylist[[1]] gives what you would refer to as "janindex" and mylist[[2]] gives febindex... It would make so much more sense and be approximately 8923546325345432234532 times cleaner.

    Heck we could even use month.abb or month.name to give meaningful names to each list element...

    Code: 
    index <- vector("list", 12)
    names(index) <- month.abb
    index[["Jan"]]
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  6. #6
    Points: 136, Level: 2
    Level completed: 72%, Points required for next Level: 14

    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: create or rename variables with assign

    Quote Originally Posted by Dason View Post
    I still don't understand why you want variables named "janindex" and "febindex" instead of having a master list so that mylist[[1]] gives what you would refer to as "janindex" and mylist[[2]] gives febindex... It would make so much more sense and be approximately 8923546325345432234532 times cleaner.

    Heck we could even use month.abb or month.name to give meaningful names to each list element...

    Code: 
    index <- vector("list", 12)
    names(index) <- month.abb
    index[["Jan"]]
    I'd welcome a different approach, I'm just not sure how to do it. Basically, I have a time-series of length 14,000 or so and I'm interested in a few scattered (and of unequal length) intervals within that time-series. I already know where these intervals are, but I want an easy way to call the index number since I need it for another program. These intervals are extremely small compared to the length of the time-series (e.g., length 60 or so) and correspond to my index1,index2,...,index12. Then, within each of those groups, I'd like to be able to extract the index number for every 12th element.

  7. #7
    RotParaTon
    Points: 46,091, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterActivity AwardCommunity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,061
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    Re: create or rename variables with assign

    If you want to grab every 12th element you can easily construct a sequence to do that
    Code: 
    > j <- 1:200
    > j[seq(12, length(j), by = 12)]
     [1]  12  24  36  48  60  72  84  96 108 120 132 144 156 168 180 192
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  8. #8
    Points: 136, Level: 2
    Level completed: 72%, Points required for next Level: 14

    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: create or rename variables with assign


    Quote Originally Posted by Dason View Post
    If you want to grab every 12th element you can easily construct a sequence to do that
    Code: 
    > j <- 1:200
    > j[seq(12, length(j), by = 12)]
     [1]  12  24  36  48  60  72  84  96 108 120 132 144 156 168 180 192
    This isn't really the major obstacle I'm facing though. Here's my situation in more detail:

    I have a time-series of roughly 14,000 months going back to 800 AD. Scattered within that time-series are several (maybe 20) intervals that I'm interested in, each interval of maybe 3-4 years long, that I have already defined index numbers to. For example if I'm interested in the intervals 950-952 AD and 1076 to 1080 AD, then *so far what I have done* is:

    Code: 
    startindex1=(950-800)*12+1 # index for january 950
    endindex1=(952-800)*12+12
    startindex2=(1076-800)*12+1
    endindex2=(1080-800)*12+12
    
    index1=startindex1:endindex1
    index2=startindex2:endindex2
    
    i=0 # Get januaries, februaries, etc, this is similar to what you did above
    janindex1=index1[1:(i+12)==(i+1)]
    febindex1=[1:(i+12)==(i+2)]
    ## This process needs to be repeated 12 times the number of "intervals" I'm interested in
    There are a couple reasons why I aimed toward a simple variable name like "novindex5." One is I need those index numbers to plug into some netCDF based files I am using, which are climate files. These files are independent of the time series I am working with but begin with the same month. The way I extract a variable from those files is by location and time, so for example if I wanted to find the temperature in the 780th month in the time-series I'd be able to just type temp[location,780]. Or if I wanted to make a december-january-february average temperature for 950-952 AD interest, I could just say mean(temp[location,c(janindex1,febindex1,decindex1)]), etc.

    If there's a much easier way to go about this I'm all ears, but this seemed pretty straightforward. I was just having an issue making a loop to create all the variable names.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts








Advertise on Talk Stats