我希望我的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)
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
希望你们能帮助我!
答案 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)
答案 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循环可能有点大,可以更快地完成,但只要它工作就好了,哈哈。感谢您的快速回复和帮助!