当我单击“运行”时使用RStudio,它可以正常执行,但使用源代码,它会给我一条错误消息

时间:2018-08-19 21:21:41

标签: r

我有以下代码,这些代码经过优化,可以将结果导出到csv。一切正常,但最后一行代码

write.csv(solutionsMatrix, file="SLATE/DK.A1.CSV")

当我单击“运行”时,它会正确执行,但是当我单击“源”时,它会给我一条错误消息

source(“〜/ .active-rstudio-document”,echo = TRUE)中的错误:   〜/ .active-rstudio-document:76:34:意外的符号 75: 76:write.csv(solutionsMatrix,file =“ SLATE

向上箭头指向SLATE中的S

为什么要这样做?感谢您的关注

setwd("C:/DFS/NFL/R DATA")
library('lpSolve')
data <-read.csv ("DRAFTKINGS.CSV")

#Convert salary to numeric
data$Salary <- as.numeric(gsub(",","",data$Salary), data$Salary)
data$MAX.TEAM <- as.numeric(gsub(",","",data$MaxTeam), data$MaxTeam)

#Add binary valeus for positions  'Constraint Vectors'
data <- cbind(data, X=1)
data <- cbind(data, QB=ifelse(data$Pos=="QB",1,0))
data <- cbind(data, RB=ifelse(data$Pos=="RB", 1,0))
data <- cbind(data, WR=ifelse(data$Pos=="WR", 1,0))
data <- cbind(data, TE=ifelse(data$Pos=="TE", 1,0))
data <- cbind(data, DEF=ifelse(data$Pos=="DEF", 1,0))



#Objective Function. sum of proj pts
f.obj <- data$DKA1

#Constraints
num_X <- 9
num_QB <- 1
min_RB <- 2
min_WR <- 3
min_TE <-1
num_DEF <-1
max_RB <- 3
max_WR <-4
max_TE <-2
max_team_cost <- 50000
max_player_from_a_team <-data [2, 5]


#Constraints for max players from team
clubs <- sort(unique(data$Team))

team_constraint_vector <- c()
team_constraint_dir <- c()
team_constraint_rhs <- c()

for(i in 1:length(clubs)){
  temp <- data$Team==as.character(clubs[i])
  temp[temp==T] <- 1
  temp[temp==F] <- 0

  team_constraint_vector <- c(team_constraint_vector, temp)
  team_constraint_dir <- c(team_constraint_dir, "<=")
  team_constraint_rhs <- c(team_constraint_rhs, max_player_from_a_team)
}

solutions <- list()
solutionsMatrix <- matrix(, nrow=0, ncol=21)

for(i in 1:1){
  f.con <- matrix (c(data$X,data$QB , data$RB ,data$RB ,data$WR, data$WR,data$TE, data$TE,data$DEF , data$Salary, team_constraint_vector), nrow=(10+length(clubs)), byrow=TRUE)
  f.dir <- c("=","=", ">=","<=",">=","<=",">=","<=", "=",  "<=", team_constraint_dir)
  f.rhs <- c(num_X,num_QB,min_RB,max_RB,min_WR, max_WR,min_TE, max_TE, num_DEF, max_team_cost, team_constraint_rhs)

  x <- lp ("max", f.obj, f.con, f.dir, f.rhs, all.bin=TRUE)
  x

  solutions[[i]] <- data[which(x$solution==1),]

  solutionsMatrix <- rbind(solutionsMatrix, c("DKA1, sum(solutions[[i]]$DKA1), sum(solutions[[i]]$Salary), toString(solutions[[i]]$Name[4]),toString(solutions[[i]]$Pos[4]), toString(solutions[[i]]$Name[5]),toString(solutions[[i]]$Pos[5]), toString(solutions[[i]]$Name[8]),toString(solutions[[i]]$Pos[8]), toString(solutions[[i]]$Name[9]),toString(solutions[[i]]$Pos[9]), toString(solutions[[i]]$Name[6]),toString(solutions[[i]]$Pos[6]), toString(solutions[[i]]$Name[7]), toString(solutions[[i]]$Pos[7]),toString(solutions[[i]]$Name[2]),toString(solutions[[i]]$Pos[2]), toString(solutions[[i]]$Name[3]),toString(solutions[[i]]$Pos[3]), toString(solutions[[i]]$Name[1]),toString(solutions[[i]]$Pos[1])))


}

solutions[[1]]

#Solutions Matrix Optimization
solutionsMatrix

write.csv(solutionsMatrix, file="SLATE/DK.A1.CSV")
###################################################################

0 个答案:

没有答案