Reputation: 59
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.
Upvotes: 0
Views: 713
Reputation: 93813
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. - vector
s for a series of values, list
s for related series of values of unequal lengths, data.frame
s 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.
Upvotes: 2
Reputation: 15458
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"))))
Upvotes: 0
Reputation: 263331
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")] )
Upvotes: 5