# You can write R code here and then click "Run" to run it on our platform

library(readr)
library(rpart)
# The competition datafiles are in the directory ../input
# Read competition data files:
train <- read_csv("../input/train.csv")
test <- read_csv("../input/test.csv")


# Write to the log:
cat(sprintf("Training set has %d rows and %d columns\n", nrow(train), ncol(train)))
cat(sprintf("Test set has %d rows and %d columns\n", nrow(test), ncol(test)))


train$Cover_Type <- as.factor(train$Cover_Type)
TrainHeader <- names(train)
TrainHeader <- TrainHeader[!TrainHeader %in% c("Id","Cover_Type")]
TrainHeader <- paste(TrainHeader, collapse = "+")
Trainform <- as.formula(paste("Cover_Type", TrainHeader, sep = " ~ "))

cat(sprintf("building tree"))


tree <- rpart(Trainform,data=train,method="class")
printcp(tree)
cat(sprintf("tree done"))
#cat(new)

res <- predict(tree,test, type="class")
# Generate output files with write_csv(), plot() or ggplot()
# Any files you write to the current directory get shown as outputs
cat(sprintf("predict done"))

done <- data.frame(Id = test$Id, Cover_Type = as.character(res))

write_csv(done,"./res.csv")

cat(sprintf("Done"))

