订购和绑定循环r



我希望我的循环根据for循环中指定的正确顺序将数据存储在行中,但是以某种方式,数据在新的数据范围内随机绑定。

背景信息:

  • 我有一个数据框,其中包含来自FMCG行业的数据,其中包括产品的价格。
  • 我的数据帧包括55个不同产品类别的前三名品牌
  • 在我的计量经济学(营销)模型中,我也可以接受竞争,因此我要做的就是根据条件计算品牌A,B和C的平均价格。
  • 当我运行一个类别的循环时,例如类别5(从我的总数据集)中,我可以按顺序获得正确的平均值并堆叠这些平均值,因此我可以将它们绑定到我的主数据帧中。该主要数据框架每周都有从上到下列出的所有品牌。
  • 问题:当我运行for循环时,它确实会计算平均值,但是最终的堆叠数据框架可以绑定所有值,以使所有值不再顺序。
  • 所以,我需要找到一个命令,使我计算得的平均值(因此,竞争的平均竞赛类别5品牌每周为第一行,平均第550个品牌C周208,这是最后一次),就像我一样为一个类别做,但不在for循环中。

代码:

##for loop for all competitor average prices accross all categories
    for(X in c("5", "24", "32", "43", "49", "56", "63", "81", "94", "96", "102", "105", "115", "122", "129", "133", "145", "154", "180", "189", "201", "210", "219", "226", "231", "245", "264", "277", "280", "301", "313", "335", "346", "361", "397", "409", "410", "411", "413", "437", "443", "480", "488", "493", "500", "516", "526", "533", "535", "536", "542", "543", "549", "550")){
  PriceX <- read.table(paste0("/Users/censored",X,".csv"), header=TRUE, sep=",")
  PriceX <- transpose(PriceX)
  PriceX <- PriceX[-c(1), ] #deleting the first column of df, contains brand names
  PriceX <- transpose(PriceX)
  PriceX <- sapply(PriceX, as.numeric)
  PriceX <- data.frame(PriceX)
  CompPriceXa <- PriceX[-c(1:2), ] #delete first and second row, contains totals and Brand A
  CompPriceXa <- CompPriceXa %>%  mutate_if(is.numeric, funs(ifelse(. == 0, NA, .))) #Make all zeros NA, otherwise the means are calculated over rows. 
  CompPriceXa <- colMeans(CompPriceXa, na.rm=T) #calculate the mean of competitor prices when Brand A is focal brand
  CompPriceXa <- as.matrix(CompPriceXa)
  CompPriceXb <- PriceX[-c(1, 3), ]
   CompPriceXb <- CompPriceXb %>%  mutate_if(is.numeric, funs(ifelse(. == 0, NA, .)))
  CompPriceXb <- colMeans(CompPriceXb, na.rm=T)
  CompPriceXb <- as.matrix(CompPriceXb)
  CompPriceXc <- PriceX[-c(1, 4), ]
  CompPriceXc <- CompPriceXc %>%  mutate_if(is.numeric, funs(ifelse(. == 0, NA, .)))
  CompPriceXc <- colMeans(CompPriceXc, na.rm=T)
  CompPriceXc <- as.matrix(CompPriceXc)
  StackedCompPrice <- rbind(CompPriceXa, CompPriceXb, CompPriceXc) #stack the average competitor prices of Brand A, B and C. 
  StackedCompPrice <- as.data.frame(StackedCompPrice)
  ALLStackedCompPrice <- rbind(StackedCompPrice, ALLStackedCompPrice)}
ALLStackedCompPrice <- StackedCompPrice[NULL,] #first run the for loop, then run this command outside loop to create empty df, then run the forloop again and it will be filled with all the values from all categories

希望你们能帮助我!

如果您嵌套了另一个循环,并将其放在要修改的数据框架索引中,则要修改应解决问题。显然,没有数据,这个答案只是指导而不是具体代码...

    ##for loop for all competitor average prices accross all categories
        for(X in c("5", "24", "32", "43", "49", "56", "63", "81", "94", "96", "102", "105", "115", "122", "129", "133", "145", "154", "180", "189", "201", "210", "219", "226", "231", "245", "264", "277", "280", "301", "313", "335", "346", "361", "397", "409", "410", "411", "413", "437", "443", "480", "488", "493", "500", "516", "526", "533", "535", "536", "542", "543", "549", "550")){
      PriceX <- read.table(paste0("/Users/censored",X,".csv"), header=TRUE, sep=",")
      PriceX <- transpose(PriceX)
      PriceX <- PriceX[-c(1), ] #deleting the first column of df, contains brand names
      PriceX <- transpose(PriceX)
      PriceX <- sapply(PriceX, as.numeric)
      PriceX <- data.frame(PriceX)
      CompPriceXa <- PriceX[-c(1:2), ] #delete first and second row, contains totals and Brand A
      CompPriceXa <- CompPriceXa %>%  mutate_if(is.numeric, funs(ifelse(. == 0, NA, .))) #Make all zeros NA, otherwise the means are calculated over rows. 
      CompPriceXa <- colMeans(CompPriceXa, na.rm=T) #calculate the mean of competitor prices when Brand A is focal brand
      CompPriceXa <- as.matrix(CompPriceXa)
      CompPriceXb <- PriceX[-c(1, 3), ]
       CompPriceXb <- CompPriceXb %>%  mutate_if(is.numeric, funs(ifelse(. == 0, NA, .)))
      CompPriceXb <- colMeans(CompPriceXb, na.rm=T)
      CompPriceXb <- as.matrix(CompPriceXb)
      CompPriceXc <- PriceX[-c(1, 4), ]
      CompPriceXc <- CompPriceXc %>%  mutate_if(is.numeric, funs(ifelse(. == 0, NA, .)))
      CompPriceXc <- colMeans(CompPriceXc, na.rm=T)
      CompPriceXc <- as.matrix(CompPriceXc)
      StackedCompPrice <- rbind(CompPriceXa, CompPriceXb, CompPriceXc) #stack the average competitor prices of Brand A, B and C. 
      StackedCompPrice <- as.data.frame(StackedCompPrice)
for(i in 1:5{
      ALLStackedCompPrice[,i]<- rbind(StackedCompPrice, ALLStackedCompPrice)
}}
    ALLStackedCompPrice <- StackedCompPrice[NULL,] #first run the for loop, then run this command outside loop to create empty df, then run the forloop again and it will be filled with all the values from all categories

如您在链接中所看到的,我已经制作了我的数据范围的屏幕截图,为您提供了我的数据插图。

主数据帧

计算数据集

平均作为输出

这将给人以数据集的印象:

x var1 var2var3 var4

1-1 6.584001 6.618493 6.669796 6.14605

2-1 6.316876 6.299771 6.264874 5.531244

3-13.9143013.9538273.9558413.640814

4-13.6293023.6559623.6570913.525953

5-14.8019134.7911464.8191354.888309

解决了整个问题!这是非常简单的事情...只需逆转for循环,因此" 550"类别为" 550",而" 5"是最后一个,然后就可以了。for循环也许有点大,可以更快地完成,但是只要它可以正常工作,哈哈。感谢您的快速回复和帮助!

最新更新