-1

I hope you can help me. I am currently trying to load and create several datsets using a for loop. The loop is supposed to load different datsets for a total of 62 categories. However, it does not really substitute the X by the numbers, but only creates datasetX.

I hope I was able to explain my issues. Here is my code:

##### Defining brands and START LOOP ####
for(X in c("8", "34", "35", "39", "44", "48", "49", "50", "51", "52", "53", "54", "55", "56", "120", "134", "138", "181", "182", "187", "190", "192", "199", "200", "205", "212", "215", "271", "274", "279", "329", "330", "331", "332", "333", "334", "335", "344", "345", "346", "349", "350", "351", "371", "372", "410", "411", "412", "413", "414", "418", "429", "443", "444", "455", "456", "527", "528", "529", "530", "534", "559")){

#Display
DisplayX <- read_excel("~/Master Studium/Thesis/IRI/displayOnly/DisplayOnly_Category8.xlsx")
DisplayX <- transpose(DisplayX)
DisplayX <- DisplayX[-c(1), ]
DisplayX$V1 <- NULL
DisplayX$Week <- as.matrix(c(1:208))

#sales
SalesX <- read_excel("~/Master Studium/Thesis/IRI/volumeSales/volumeSales_Category8.xlsx")
SalesX <- transpose(SalesX)
SalesX <- SalesX[-c(1), ]
SalesX$V1 <- NULL
SalesX$Week <- as.matrix(c(1:208))

#price
PriceX <- read_excel("~/Master Studium/Thesis/IRI/price/Price_Category8.xlsx")
PriceX <- transpose(PriceX)
PriceX <- PriceX[-c(1), ]
PriceX$V1 <- NULL
PriceX$Week <- as.matrix(c(1:208))

#advertising
AdvertisingX <- read_excel("~/Master Studium/Thesis/IRI/advertising/Advertising_Category8.xlsx")
AdvertisingX <- transpose(AdvertisingX)
AdvertisingX <- AdvertisingX[-c(1), ]
AdvertisingX$V1 <- NULL
AdvertisingX$Week <- as.matrix(c(1:208))

#importing brand 1 into a dataframe 
CategoryXa <- as.matrix(c(1:208))
CategoryXa <- as.data.frame(CategoryXa)
names(CategoryXa)[names(CategoryXa)=="V1"] <- "Week"
CategoryXa$Category <- X
CategoryXa$Brand <- 2
CategoryXa$Display <- DisplayX$V2
CategoryXa$Sales <- SalesX$V2
CategoryXa$Price <- PriceX$V2
CategoryXa$Advertising <- AdvertisingX$V2

#importing brand 2 into a dataframe 
CategoryXb <- as.matrix(c(1:208))
CategoryXb <- as.data.frame(CategoryXb)
names(CategoryXb)[names(CategoryXb)=="V1"] <- "Week"
CategoryXb$Category <- X
CategoryXb$Brand <- 3
CategoryXb$Display <- DisplayX$V3
CategoryXb$Sales <- SalesX$V3
CategoryXb$Price <- PriceX$V3
CategoryXb$Advertising <- AdvertisingX$V3

#importing brand 3 into a dataframe
CategoryXc <- as.matrix(c(1:208))
CategoryXc <- as.data.frame(CategoryXc)
names(CategoryXc)[names(CategoryXc)=="V1"] <- "Week"
CategoryXc$Category <- X
CategoryXc$Brand <- 4
CategoryXc$Display <- DisplayX$V4
CategoryXc$Sales <- SalesX$V4
CategoryXc$Price <- PriceX$V4
CategoryXc$Advertising <- AdvertisingX$V4

CategoryX <- rbind(CategoryXa, CategoryXb, CategoryXc) 
}

So instead of having the datasets Category8, Category34, Category35 and so on, I only end up with CategoryX.

Thank you for your help!

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Amy
  • 11
  • 1
  • Welcome to SO! Please use built-in or otherwise reproducible data in your example so that we can reproduce the problem and fix it for you https://stackoverflow.com/help/mcve – Hack-R Apr 30 '18 at 15:05
  • 3
    Use lists. You shouldn't want sequentially named data sets. I'd suggest reading [my answer for "How to make a list of data frames"](https://stackoverflow.com/a/24376207/903061) for an explanation and discussion. – Gregor Thomas Apr 30 '18 at 15:07
  • Also, be glad that the X isn't subsituted in your names. Think how terrible it would be if you had `a <- 523` in your environment and then all your "Category" names became "C523tegory"! You'd have to be impossibly careful with names. – Gregor Thomas Apr 30 '18 at 15:09
  • As @Gregor pointed out, it might not be so useful to create so many variables. Nevertheless the solution to defining new variables in every iteration of your loop is to use `assign("name_of_the_new_variable",value_of_the_variable, envir=.GlobalEnv)`. – jkd Apr 30 '18 at 16:22
  • Hallo everyone, thank you for your comments and answers. I will read into your ideas and see if i can get in running. Thank you! – Amy May 01 '18 at 07:38

1 Answers1

0

Here a short example of how to define new variables from inside a loop:

for (i in 1:10) {
  assign(paste("newVar",i,sep=""), i, envir = .GlobalEnv)
}
jkd
  • 1,327
  • 14
  • 29