If () statement in R


I am fairly new in R and have question on if () statement in R. Any help is greatly appreciated. My data look like as listed below

Var1 var2 01 A 02 D 03 E 04 K

I need to create 2 additional variables (Var3 and Var4) based on var1. In SAS I would create var3 and var4 in following way:

if var1 = 01 then do; var3 = "New York"; var4= "NYC"; end; else; if var1 = 02 then do; var3 = "Illinois"; var4= "ORD"; end; else; if var1 = 03 then do; var3 = "Texas"; var4= "DFW"; end; else; if var1 = 04 then do; var3 = "New Jersey"; var4= "EWR"; end; else;

I want to convert above if statements into R program. Any help is greatly appreciated.


var3 <- c("New York", "Illinois", "Texas", "New Jersey")[ match(var1, c("01","02","03","04")] var4 <- c("NYC", "ORD", "DFW", "EWR")[ match(var1, c("01","02","03","04")]

Generally the items you will be working with are dataframes rather than the Sets you have in SAS-world. So you would have a data-object name, say dat and you might execute:

dat$var4 <- with( dat, c("NYC", "ORD", "DFW", "EWR")[ match(var1, c("01","02","03","04")] )


The basic R rule I impose on myself is to store data that you might have to call upon in an appropriate structure (e.g. - vectors for a series of values, lists for related series of values of unequal lengths, data.frames for related series of equal lengths etc etc).

So an appropriate structure in this case is a data.frame for related series of the same length. E.g.:

srcs <- data.frame( Var1=c("01","02","03","04"), state=c("New York", "Illinois", "Texas", "New Jersey"), code=c("NYC", "ORD", "DFW", "EWR") )

Then use that store of basic information to add new variables as appropriate, e.g.:

dat <- cbind(dat, srcs[c("state","code")][match(dat$Var1,srcs$Var1),] ) # Var1 var2 state code #1 01 A New York NYC #2 02 D Illinois ORD #3 03 E Texas DFW #4 04 K New Jersey EWR

This turns a whole bunch of if/ else statements into a single call, and avoids the need for ugly looping operations.


Another solution is to use ifelse (assuming that you have only four values for var1).

mydata$var3<- with (mydata, ifelse(var1 =="01", "New York" , ifelse (var1 == "02", "Illinois", ifelse (var1 == "03","Texas" ,"New Jersey" )))) mydata$var4<-with (mydata, ifelse (var1 =="01", "NYC", ifelse (var1 == "02","ORD", ifelse (var1 == "03", "DFW","EWR"))))


  • If () statement in R
  • CSS same height as Parent
  • Why does `ordinality(of: .day, in: .era, for: date)` give the same result for 2 dates in different t
  • PHP Script for States to Abbreviations Not Working Correctly
  • Converting JSON string to DataTable
  • Can I have SEO friendly urls in Struts?
  • Keep checkbox checked after form submit
  • How to make Istanbul generate coverage for all of my source code?
  • Pull to refresh in Angular Js
  • Declare a new variable for each iteration in a for loop
  • Vendor agnostic SQL to concatenate field values across records
  • Word cloud in R with two separate values
  • Failing to load properties file in an Eclipse - JUnit test
  • Django CBV: Easy access to url parameters in get_context_data()?
  • Google Drive: Move file to folder
  • Extracting specific pattern from nmap
  • How to convert this 'for' loop to a vector solution
  • How do I set an environment variable for airflow to use?
  • Git push to GitHub failing - seems to be trying to use HTTP?
  • How do I ensure that the email app will use my app to open a file?
  • C Programming: Preprocessor, macros as tokens
  • How to combine 2 variables and ignore NAs
  • Caesar Cipher validation
  • How to handle Service unavailable scenarios with Jersey REST
  • Pure JerseyTest without letting Spring messing with services
  • Retrieve RSS feed and display it in text view
  • Sign Key HMAC SHA1 with Javascript
  • geom_map “map_id” reference issue
  • Python: I fell in an infinite loop
  • Getting access to a binary response byte-by-byte in classic asp/JScript
  • Jersey REST api multiple resources
  • T-SQL: cross apply; too many iterations on line items
  • What does the lower left pane in OllyDbg displays?
  • php regex remove digits
  • Does anyone have a Categorized XML Corpus Reader for NLTK?
  • Vigenere cipher not working
  • jersey/tomcat Description The origin server did not find a current representation for the target res