通过 for 循环和函数附加到数据框

Append to dataframe through for loop and function

当我 运行 代码最后另存为 csv 时,一切 运行 都正确。这意味着我按国家过滤我的数据集做一些延续并将其保存为国家文件。 当我尝试 rbind 我的数据集时,我什么也没收到。我尝试了我发现的不同解决方案,但没有任何效果。显然我不明白为什么我不能 rbind。

代码:

library(dplyr)
library(readxl)

setwd("Z:/Reporting_Private/Tableau")
dataupl <- read_excel("Analysis Map_Tableau - Ready.xlsm", sheet = "Data")

df = dataupl

#select right columns 
df = df[,1:6]
colnames(df)=c("Office", "Employee","Territiry","Sales","Leads","Act")

#change n/a to zero
df[is.na(df)]=0

countries = df %>% select(Office) %>% distinct()
countries = as.data.frame(countries)

engine <- function(input){

  df = df %>% filter(Office==input)

  SCALESALES = scale(df$Sales)
  SCALELEADS= scale(df$Leads)
  SCALEACT= scale(df$Act)

  df = df %>% mutate(SCALESALES = SCALESALES, SCALELEADS = SCALELEADS, SCALEACT = SCALEACT)

  df$SLegend = ave(df$Sales, df$SalesLegend, FUN = min)   
  df$SLegend = ifelse(df$SLegend>0, df$SLegend,0)
  df$LLegend = ave(df$Leads, df$LeadsLegend, FUN = min)   
  df$ALegend = ave(df$Act, df$ActLegend, FUN = min)   


  #write.csv(final, file = paste0(input,".csv"))
  dftotal = data.frame()
  dftotal = rbind(dftotal,df)
}

for (i in 1:nrow(countries)){

  input = countries[i,]

  engine(input)

}

您似乎可以拆分数据框 L <- split(df, df$Office),然后 lapply(L, ...) 您必须 return 数据框,而不是 write.csv(...)return(df) 在您的功能。
所以,像这样:

engine <- function(dfi) {

  SCALESALES = scale(dfi$Sales)
  SCALELEADS = scale(dfi$Leads)
  SCALEACT   = scale(dfi$Act)

  dfi = dfi %>% mutate(SCALESALES = SCALESALES, SCALELEADS = SCALELEADS, SCALEACT = SCALEACT)

  dfi$SLegend = ave(dfi$Sales, dfi$SalesLegend, FUN = min)   
  dfi$SLegend = ifelse(dfi$SLegend>0, dfi$SLegend, 0)
  dfi$LLegend = ave(dfi$Leads, dfi$LeadsLegend, FUN = min)   
  dfi$ALegend = ave(dfi$Act, dfi$ActLegend, FUN = min)   

  return(dfi)
}

L    <- split(df, df$Office)
Lnew <- lapply(L, engine)

dftotal <- Lnew[[1]]
for (i in 2:length(Lnew)) dftotal <- rbind(dftotal, Lnew[[i]])