Re: [R] Assign name to a name

From: Stephen Tucker <brown_emu_at_yahoo.com>
Date: Fri, 29 Jun 2007 22:20:06 -0700 (PDT)


You can just create another variable which contains the "names" you want:

## let

Year <- c(rep(1999,2),rep(2000,2),rep(2001,3))

## one alternative

getYearCode1 <- function(yr) {
  # yr can be a vector
  ifelse(yr==1999,"Year1",

         ifelse(yr==2000,"Year2",
                ifelse(yr==2001,"Year3")))
}

## another alternative
## more appropriate since you probably want
## a single value returned

getYearCode2 <- function(yr) {
  # yr is a single value

  switch(as.character(yr),
         `1999` = "Year1",
         `2000` = "Year2",
         `2001` = "Year3")

}

## Application:
## single value

getYearCode1(Year[1])
getYearCode2(Year[1])
## on a vector

dataset$YearCode <- getYearCode1(Year)
# or
dataset$YearCode <- sapply(Year,getYearCode2)

## another option is match()

df <- data.frame(Year=c(1999,2000,2001),YearCode=c("Year1","Year2","Year3")) dataset$YearCode <- df[match(Year,df[,"Year"]),"YearCode"]

##========================================================

## reading from console

subset(dataset,YearCode==scan("",what="")) subset(dataset,

   YearCode=={x <- function() {cat("YrCode: ");readline("")}; x()})

## or as a function

f <- function(x) {
  g <- function() {
    x <- function() {

      cat("YearCode: ");
      readline("")

    }
    subset(dataset,YearCode==x())
  }
}
getSubset1 <- f(dataset)

## type at console. You will be prompted:
datayear <- getSubset1()

## but easier is

f <- function(x) {
  g <- function(y)
    subset(x,YearCode==y)
}
getSubset2 <- f(dataset)

## type at prompt

datayear <- getSubset1(1999)

> I would like to know how I can assign a name to a name.  I have a
> dataset that has different years in it.  I am writing scripts using R
> and I would like to give a month a generic name and then use the generic
> name to do different analysis.  The reason for the generic name would be
> so that I only have to change one thing if I wanted to change the year.
> For example.
> Year1 = 1999
> datayear <- subset(dataset, Year = Year1)
> I would want  to subset for whatever year is in "Year1".  I am not sure
> if R does this but it would be great if it does.  Is there also anyway
> for R to ask the user for the variable in the console without going into
> the script and then use whatever the user puts in. Thanks for the help.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help_at_stat.math.ethz.ch 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.
>

______________________________________________
R-help_at_stat.math.ethz.ch 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. Received on Sat 30 Jun 2007 - 05:26:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 30 Jun 2007 - 05:32:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.