I am ashamed to be asking this question, but I couldn't find the solution anywhere. Searching for "if" and "R" is not very productive... I cannot get a simple if statement to work. I have data on college students. I want to make a string variable that has the names of the years. That is, when the year variable i is equal to 1, I want to have a variable called years equal to "Freshmen". I tried this years <- "Freshmen" if i==1 years <- "Sophomores" if i==2 and so on, but I couldn't get it to work. How can I get this variable to work? Thanks, BQ
How about the following, if you really want characters or just leave as factor i <- round(runif(10, 1, 4)) years <- as.character(factor(i, labels = c("Freshman", "Sophomore", "Junior", "Senior"))) HTH, ken
On Fri, 7 Apr 2006, Brian Quinif wrote:> I am ashamed to be asking this question, but I couldn't find the > solution anywhere. Searching for "if" and "R" is not very > productive... > > I cannot get a simple if statement to work. > > I have data on college students. I want to make a string variable > that has the names of the years. That is, when the year variable i is > equal to 1, I want to have a variable called years equal to > "Freshmen". > > I tried this > years <- "Freshmen" if i==1 > years <- "Sophomores" if i==2 > > and so on, but I couldn't get it to work. How can I get this variable to work?Most simply: years <- c("Freshmen", "Sophomores")[i] What you seem to be trying to do can be written if(i == 1) years <- "Freshmen" if(i == 2) years <- "Sophomores" but then 'years' is undefined if !i %in% c(1,2). Better ways to program that are years <- switch(i, "Freshmen", "Sophomores") (which gives NULL otherwise) or years <- if(i == 1) "Freshmen" elseif(i == 2) "Sophomores" else "unknown" of (vectorized) years <- ifelse(i == 1, "Freshmen", ifelse(i == 2, "Sophomores", "unknown")) But the first solution is both vectorized and simple. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Fri, Apr 07, 2006 at 02:58:00AM -0400, Brian Quinif wrote:> > I have data on college students. I want to make a string variable > that has the names of the years. That is, when the year variable i is > equal to 1, I want to have a variable called years equal to > "Freshmen". > > I tried this > years <- "Freshmen" if i==1 > years <- "Sophomores" if i==2What you are looking for is not an if clause but logical indexing: years[years=="Freshmen"] <- 1 years[years=="Sophomores"] <- 2 Of course it's still a character vector so you will have to do years = as.numeric(years) Have a look at the manual (Introduciotn to R) for more details. Another question is what you have in mind. To me it looks like what you are trying to do is make a factor on your own. Maybe this is what you want: factor(years) or maybe factor(years, levels=c("Freshmen", "Sophomores")) if you want more control over the coding. cu Philipp -- Dr. Philipp Pagel Tel. +49-8161-71 2131 Dept. of Genome Oriented Bioinformatics Fax. +49-8161-71 2186 Technical University of Munich Science Center Weihenstephan 85350 Freising, Germany and Institute for Bioinformatics / MIPS Tel. +49-89-3187 3675 GSF - National Research Center Fax. +49-89-3187 3585 for Environment and Health Ingolst?dter Landstrasse 1 85764 Neuherberg, Germany http://mips.gsf.de/staff/pagel