for循环中的排序和绑定r

时间:2017-12-08 12:42:29

标签: r for-loop mean

我希望我的for循环基于for循环中指定的正确顺序将数据存储在行中,但不知何故,数据在新数据帧中随机绑定。

背景资料:

  • 我有一个数据框,其中包含来自快速消费品行业的数据,其中包括产品价格。
  • 我的数据框由55个不同产品类别的前3个品牌组成
  • 在我的计量经济学(市场营销)模型中,我也适应竞争,所以我所做的是根据条件计算品牌A,B和C的竞争平均价格。
  • 当我为一个类别运行循环时,比如类别5(来自我的整个数据集),我按顺序得到正确的平均值并堆叠这些,所以我可以将它们绑定到我的主数据帧。此主数据框包含从上到下列出的所有品牌,每个类别,每周。
  • 问题:当我运行for循环时,它确实计算了平均值,但是最终堆叠的数据帧将对所有值进行混洗,使得它们不再有序。
  • 因此,我需要找到一个命令,使我的计算平均值保持有序(因此竞争类别5品牌A第1周作为第一行的平均值和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

希望你们能帮助我!

4 个答案:

答案 0 :(得分:0)

如果你嵌套另一个for循环,并将那个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)
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

答案 1 :(得分:0)

正如您在链接中看到的,我已经制作了数据框的屏幕截图,以便您了解我的数据。

Main dataframe

Dataset for calculations

Averages as output

答案 2 :(得分:0)

这将给出我进行计算的数据集的印象:

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-1 3.914301 3.953827 3.955841 3.640814

4-1 3.629302 3.655962 3.657091 3.525953

5-1 4.801913 4.791146 4.819135 4.888309

答案 3 :(得分:0)

整个问题解决了!这是非常简单的事情......只需颠倒for循环,所以类别'550'作为第一个,'5'作为最后一个,你去。 ,for循环可能有点大,可以更快地完成,但只要它工作就好了,哈哈。感谢您的快速回复和帮助!