I have a data frame that’s ~ 50,000 X 200. The column names are 4 different types with numbers on the end ranging from 1-50 (store1, price1, time1, rate1, store2, price2, time2, rate2,…,store50, price50, time50, rate50). I’m trying to create dummy variables depending on the values of each column but am having trouble getting R to handle the column names inside a loop.
store1 price1 time1 rate1 store2 price2 time2 rate2 ....
A 55.55 08:09 1.44 B 44.44 11:09 1.46
C 55.55 08:09 1.44 G 44.44 11:09 1.46
X 55.55 08:09 1.44 E 44.44 11:09 1.46
D 55.55 08:09 1.44 S 44.44 11:09 1.46
Here’s what I have tried so far with no luck.
xform_data <- function(x) {
for(i in 1:50){
storeX <- (paste("store",i,sep=""))
storeX2 <- ifelse(storeX == "A", 1, 2)
x <- cbind(x, storeX2 )
}
x
}
Any suggestions?
The following compares the name instead of the comparing the value:
Try:
Also, all the new columns will be called
storeX2. You might prefer to rename them:(I am sure there exist more elegant ways to do it.)