Reputation: 197
I am trying to create four columns in an existing data frame based on four variables. Since they all use the same logic I decided to do a loop instead of copying and pasting the code 4 times. However, I'm having trouble with the loop. Below is the code I wrote for the loop.
names<-c("a 1", "b 1", "c 1", "d 1")
for (k in names){
Large_Data$column_k <- ifelse(Large_Data$`k`== "I-2", 2,
+ ifelse(Large_Data$`k`== "I-3", 3,
+ ifelse(Large_Data$`k`== "I-4", 4,
+ ifelse(Large_Data$`k`== "I-5", 5,
+ ifelse(Large_Data$`k`== "I-6", 6,
+ ifelse(Large_Data$`k`== "I-7", 7,
+ ifelse(Large_Data$`k`== "S-1", 8,
+ ifelse(Large_Data$`k`== "S-2", 9,
+ ifelse(Large_Data$`k`== "S-3", 10,
+ ifelse(Large_Data$`k`== "S-4", 11,
+ ifelse(Large_Data$`k`== "P-1", 12,
+ ifelse(Large_Data$`k`== "P-2", 13,
+ ifelse(Large_Data$`k`== "P-3", 14,
+ ifelse(Large_Data$`k`== "D-1", 15,
+ ifelse(Large_Data$`k`== "D-2", 16,
99)))))))))))))))
}
I would appreciate any help regarding this issue. Thank you.
Upvotes: 1
Views: 74
Reputation: 680
How to build the lookup table
library(data.table)
letter=c('I','S','P','D')
start=c(2,1,1,1)
end=c(7,4,3,2)
label=2:16 # correspond to the I-2, I-3,..S1,S2,... values in that order
code.table=data.table(letter,start,end)
code.vector=unlist(apply(code.table,1,function(x) paste(x[1],x[2]:x[3],sep='-')))
lookup.table=data.table(code=code.vector,label=label)
Once the lookup table is built you can create a function that merge your table and get the label and apply this function to all the column names
getlabel=function(x) merge(Large_Data,lookup.table,by.x=x,by.y="code",all.x=TRUE,sort=F)$label
lapply(names,function(x) Large_Data[,paste(x,"label",sep="_"):=getlabel(x)])
Upvotes: 0
Reputation: 1210
It seems like your problem could be more easily solved with some sort of lookup table and merging two dataframes
instead of all of the ifelse
statements.
Example:
lookup.table = structure(list(cyl = c(4L, 6L, 8L), new = structure(c(2L, 3L,
1L), .Label = c("eight", "four", "six"), class = "factor")), .Names = c("cyl",
"new"), class = "data.frame", row.names = c(NA, -3L))
merge(mtcars,lookup.table,by="cyl")
Upvotes: 1