1

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.

Lonewolf
  • 197
  • 2
  • 13
  • 2
    I think it best if you show a few lines of input e.g. `head(mydata)` and then show a few lines of what you want. Because I'm not sure what you are doing but you've maybe gone a bit off-piste above. – Stephen Henderson Mar 23 '18 at 14:55
  • 1
    @Lonewolf, I suspect your problem can be resolved much easier than with a loop, but it would be very helpful if you could share a sample of your data – Antonios Mar 23 '18 at 14:56
  • Provide a sample of `Large_Data`. Provide a few representative rows / columns please – Frostic Mar 23 '18 at 14:57
  • 2
    You can't use string column names with `$`. Use `[` instead. Something like `for (k in names) {Large_Data[, paste0("column_", k)] <- ifelse(Large_Data[, k] == "I-3", ...` – Gregor Thomas Mar 23 '18 at 14:59
  • When you have to programatically change the names of the columns use `[[`, not `$`. Try `Large_Data[[paste0("column_", k)]]` and `Large_Data[[k]]`. – Rui Barradas Mar 23 '18 at 14:59
  • Suggested duplicate: [Dynamic column names with `$`](https://stackoverflow.com/q/18222286/903061) – Gregor Thomas Mar 23 '18 at 14:59
  • 1
    You might consider using one `factor` instead of many nested `ifelse`. `as.numeric(as.character(factor(Large_Data[, k], levels = c("I-2", "I-3", "I-4", "I-5", ....), labels = 2:16)))` – Gregor Thomas Mar 23 '18 at 15:02

2 Answers2

1

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")
desc
  • 1,190
  • 1
  • 12
  • 26
0

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)])
Frostic
  • 680
  • 4
  • 11