R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

#Load into R into a variable called T3
library(readr)
T3 <- read_csv("https://goo.gl/At238b")
## Rows: 1309 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): pclass, name, sex, ticket, cabin, embarked, boat, home.dest
## dbl (6): survived, age, sibsp, parch, fare, body
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#load all required libraries
library(mlbench)
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(e1071)
library(corrplot)
## corrplot 0.92 loaded
library(rpart)
library(rattle)
## Loading required package: tibble
## Loading required package: bitops
## Rattle: A free graphical interface for data science with R.
## Version 5.5.1 Copyright (c) 2006-2021 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
library(rpart.plot)
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
library(RColorBrewer)
library(caTools)
str(T3)
## spc_tbl_ [1,309 × 14] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ pclass   : chr [1:1309] "1st" "1st" "1st" "1st" ...
##  $ survived : num [1:1309] 1 1 0 0 0 1 1 0 1 0 ...
##  $ name     : chr [1:1309] "Allen, Miss. Elisabeth Walton" "Allison, Master. Hudson Trevor" "Allison, Miss. Helen Loraine" "Allison, Mr. Hudson Joshua Creighton" ...
##  $ sex      : chr [1:1309] "female" "male" "female" "male" ...
##  $ age      : num [1:1309] 29 0.92 2 30 25 48 63 39 53 71 ...
##  $ sibsp    : num [1:1309] 0 1 1 1 1 0 1 0 2 0 ...
##  $ parch    : num [1:1309] 0 2 2 2 2 0 0 0 0 0 ...
##  $ ticket   : chr [1:1309] "24160" "113781" "113781" "113781" ...
##  $ fare     : num [1:1309] 211 152 152 152 152 ...
##  $ cabin    : chr [1:1309] "B5" "C22 C26" "C22 C26" "C22 C26" ...
##  $ embarked : chr [1:1309] "S" "S" "S" "S" ...
##  $ boat     : chr [1:1309] "2" "11" NA NA ...
##  $ body     : num [1:1309] NA NA NA 135 NA NA NA NA NA 22 ...
##  $ home.dest: chr [1:1309] "St Louis, MO" "Montreal, PQ / Chesterville, ON" "Montreal, PQ / Chesterville, ON" "Montreal, PQ / Chesterville, ON" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   pclass = col_character(),
##   ..   survived = col_double(),
##   ..   name = col_character(),
##   ..   sex = col_character(),
##   ..   age = col_double(),
##   ..   sibsp = col_double(),
##   ..   parch = col_double(),
##   ..   ticket = col_character(),
##   ..   fare = col_double(),
##   ..   cabin = col_character(),
##   ..   embarked = col_character(),
##   ..   boat = col_character(),
##   ..   body = col_double(),
##   ..   home.dest = col_character()
##   .. )
##  - attr(*, "problems")=<externalptr>
#3. Build a new dataset, titanic, by selecting these features:survived, embarked, sex, sibsp, parch and fare
titanic <- T3[, c("survived", "embarked", "sex", "sibsp","age", "parch", "fare")]
str(titanic)
## tibble [1,309 × 7] (S3: tbl_df/tbl/data.frame)
##  $ survived: num [1:1309] 1 1 0 0 0 1 1 0 1 0 ...
##  $ embarked: chr [1:1309] "S" "S" "S" "S" ...
##  $ sex     : chr [1:1309] "female" "male" "female" "male" ...
##  $ sibsp   : num [1:1309] 0 1 1 1 1 0 1 0 2 0 ...
##  $ age     : num [1:1309] 29 0.92 2 30 25 48 63 39 53 71 ...
##  $ parch   : num [1:1309] 0 2 2 2 2 0 0 0 0 0 ...
##  $ fare    : num [1:1309] 211 152 152 152 152 ...
#removing null values
any(is.na(titanic))
## [1] TRUE
titanic<-na.omit(titanic)
str(titanic)
## tibble [1,043 × 7] (S3: tbl_df/tbl/data.frame)
##  $ survived: num [1:1043] 1 1 0 0 0 1 1 0 1 0 ...
##  $ embarked: chr [1:1043] "S" "S" "S" "S" ...
##  $ sex     : chr [1:1043] "female" "male" "female" "male" ...
##  $ sibsp   : num [1:1043] 0 1 1 1 1 0 1 0 2 0 ...
##  $ age     : num [1:1043] 29 0.92 2 30 25 48 63 39 53 71 ...
##  $ parch   : num [1:1043] 0 2 2 2 2 0 0 0 0 0 ...
##  $ fare    : num [1:1043] 211 152 152 152 152 ...
##  - attr(*, "na.action")= 'omit' Named int [1:266] 16 38 41 47 60 70 71 75 81 107 ...
##   ..- attr(*, "names")= chr [1:266] "16" "38" "41" "47" ...
#Perform a statistical analysis of the titanic dataset
# display first 20 rows of data
head(titanic, n=20)
## # A tibble: 20 × 7
##    survived embarked sex    sibsp   age parch  fare
##       <dbl> <chr>    <chr>  <dbl> <dbl> <dbl> <dbl>
##  1        1 S        female     0 29        0 211. 
##  2        1 S        male       1  0.92     2 152. 
##  3        0 S        female     1  2        2 152. 
##  4        0 S        male       1 30        2 152. 
##  5        0 S        female     1 25        2 152. 
##  6        1 S        male       0 48        0  26.6
##  7        1 S        female     1 63        0  78.0
##  8        0 S        male       0 39        0   0  
##  9        1 S        female     2 53        0  51.5
## 10        0 C        male       0 71        0  49.5
## 11        0 C        male       1 47        0 228. 
## 12        1 C        female     1 18        0 228. 
## 13        1 C        female     0 24        0  69.3
## 14        1 S        female     0 26        0  78.8
## 15        1 S        male       0 80        0  30  
## 16        0 C        male       0 24        1 248. 
## 17        1 C        female     0 50        1 248. 
## 18        1 C        female     0 32        0  76.3
## 19        0 C        male       0 36        0  75.2
## 20        1 S        male       1 37        1  52.6
# display the dimensions of the dataset
dim(titanic)
## [1] 1043    7
# list types for each attribute
sapply(titanic, class)
##    survived    embarked         sex       sibsp         age       parch 
##   "numeric" "character" "character"   "numeric"   "numeric"   "numeric" 
##        fare 
##   "numeric"
#convert embarked and sex column to number in order to perform some statistical analysis
titanic$embarked <- as.numeric(factor(titanic$embarked, levels = c("S", "C")))
titanic$sex <- as.numeric(factor(titanic$sex, levels = c("male", "female")))
str(titanic)
## tibble [1,043 × 7] (S3: tbl_df/tbl/data.frame)
##  $ survived: num [1:1043] 1 1 0 0 0 1 1 0 1 0 ...
##  $ embarked: num [1:1043] 1 1 1 1 1 1 1 1 1 2 ...
##  $ sex     : num [1:1043] 2 1 2 1 2 1 2 1 2 1 ...
##  $ sibsp   : num [1:1043] 0 1 1 1 1 0 1 0 2 0 ...
##  $ age     : num [1:1043] 29 0.92 2 30 25 48 63 39 53 71 ...
##  $ parch   : num [1:1043] 0 2 2 2 2 0 0 0 0 0 ...
##  $ fare    : num [1:1043] 211 152 152 152 152 ...
##  - attr(*, "na.action")= 'omit' Named int [1:266] 16 38 41 47 60 70 71 75 81 107 ...
##   ..- attr(*, "names")= chr [1:266] "16" "38" "41" "47" ...
# distribution of class variable
y <- titanic$survived
cbind(freq=table(y), percentage=prop.table(table(y))*100)
##   freq percentage
## 0  618   59.25216
## 1  425   40.74784
#summarize the dataset
summary(titanic)
##     survived         embarked          sex           sibsp       
##  Min.   :0.0000   Min.   :1.000   Min.   :1.00   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.:1.000   1st Qu.:1.00   1st Qu.:0.0000  
##  Median :0.0000   Median :1.000   Median :1.00   Median :0.0000  
##  Mean   :0.4075   Mean   :1.213   Mean   :1.37   Mean   :0.5043  
##  3rd Qu.:1.0000   3rd Qu.:1.000   3rd Qu.:2.00   3rd Qu.:1.0000  
##  Max.   :1.0000   Max.   :2.000   Max.   :2.00   Max.   :8.0000  
##                   NA's   :50                                     
##       age            parch             fare       
##  Min.   : 0.17   Min.   :0.0000   Min.   :  0.00  
##  1st Qu.:21.00   1st Qu.:0.0000   1st Qu.:  8.05  
##  Median :28.00   Median :0.0000   Median : 15.75  
##  Mean   :29.81   Mean   :0.4219   Mean   : 36.60  
##  3rd Qu.:39.00   3rd Qu.:1.0000   3rd Qu.: 35.08  
##  Max.   :80.00   Max.   :6.0000   Max.   :512.33  
## 
# Calculate standard deviation for all attributes, handling missing values
sapply(titanic[,1:7], sd)
##   survived   embarked        sex      sibsp        age      parch       fare 
##  0.4916009         NA  0.4830592  0.9130797 14.3662545  0.8406546 55.7536477
#calculate skewness for each variable
skew <- apply(titanic[,1:7], 2, skewness)
print(skew)
##  survived  embarked       sex     sibsp       age     parch      fare 
## 0.3760484        NA 0.5373634 2.7984033 0.4057134 2.6527564 4.1106587
#Survived is the dependent variable, find its proportion in the dataset.
survived_proportion <- mean(titanic$survived == 1)
print(survived_proportion)
## [1] 0.4074784
#Find the correlation matrix between survival and the other features.
cor(x=titanic[-1], y=titanic$survived)
##                 [,1]
## embarked          NA
## sex       0.53633212
## sibsp    -0.01140343
## age      -0.05741486
## parch     0.11543601
## fare      0.24785762
cor(x=titanic$survived, y= titanic[-1])
##      embarked       sex       sibsp         age    parch      fare
## [1,]       NA 0.5363321 -0.01140343 -0.05741486 0.115436 0.2478576
#Plot  survival with other features to see if any correlation exists
titanic_cor_plot = cor(titanic)
corrplot(titanic_cor_plot)

#Make Survived embarked and sex as factors.
titanic$survived <- factor(titanic$survived)
titanic$embarked <- factor(titanic$embarked)
titanic$sex <- factor(titanic$sex)
str(titanic)
## tibble [1,043 × 7] (S3: tbl_df/tbl/data.frame)
##  $ survived: Factor w/ 2 levels "0","1": 2 2 1 1 1 2 2 1 2 1 ...
##  $ embarked: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 2 ...
##  $ sex     : Factor w/ 2 levels "1","2": 2 1 2 1 2 1 2 1 2 1 ...
##  $ sibsp   : num [1:1043] 0 1 1 1 1 0 1 0 2 0 ...
##  $ age     : num [1:1043] 29 0.92 2 30 25 48 63 39 53 71 ...
##  $ parch   : num [1:1043] 0 2 2 2 2 0 0 0 0 0 ...
##  $ fare    : num [1:1043] 211 152 152 152 152 ...
##  - attr(*, "na.action")= 'omit' Named int [1:266] 16 38 41 47 60 70 71 75 81 107 ...
##   ..- attr(*, "names")= chr [1:266] "16" "38" "41" "47" ...
#Set a seed to 1000 and use it to split the dataset into 80% training, 20% testing, 80% of the dataset is 834 and 20% is 209
set.seed(1000)
sample <- sample.split(titanic$survived, SplitRatio = 0.8) 
titanic_training  <- subset(titanic, sample == TRUE)
titanic_testing   <- subset(titanic, sample == FALSE)
#Train your learner on the training dataset and save your model in a variable  fit
fit <- rpart(survived ~ sex + age + sibsp + parch + fare + embarked,
             data=titanic_training, method="class")
#Plot your regression tree and save plot into an image file 
plot(fit)

#16)    Type the R command fancyRpartPlot(fit) to obtain a more readable plot
fancyRpartPlot(fit)

#Examine the tree you obtained. 
summary(fit)
## Call:
## rpart(formula = survived ~ sex + age + sibsp + parch + fare + 
##     embarked, data = titanic_training, method = "class")
##   n= 834 
## 
##           CP nsplit rel error    xerror       xstd
## 1 0.44411765      0 1.0000000 1.0000000 0.04173890
## 2 0.02058824      1 0.5558824 0.5558824 0.03555894
## 3 0.01470588      3 0.5147059 0.5558824 0.03555894
## 4 0.01000000      8 0.4352941 0.5441176 0.03528956
## 
## Variable importance
##   sex  fare parch sibsp   age 
##    56    19    10     9     6 
## 
## Node number 1: 834 observations,    complexity param=0.4441176
##   predicted class=0  expected loss=0.4076739  P(node) =1
##     class counts:   494   340
##    probabilities: 0.592 0.408 
##   left son=2 (521 obs) right son=3 (313 obs)
##   Primary splits:
##       sex      splits as  LR,           improve=111.480500, (0 missing)
##       fare     < 15.64585 to the left,  improve= 35.693050, (0 missing)
##       embarked splits as  LR,           improve= 18.964490, (37 missing)
##       parch    < 0.5      to the left,  improve= 16.698160, (0 missing)
##       age      < 8.5      to the right, improve=  6.584359, (0 missing)
##   Surrogate splits:
##       parch < 0.5      to the left,  agree=0.671, adj=0.125, (0 split)
##       fare  < 75.24585 to the left,  agree=0.655, adj=0.080, (0 split)
##       age   < 5.5      to the right, agree=0.629, adj=0.013, (0 split)
##       sibsp < 0.5      to the left,  agree=0.628, adj=0.010, (0 split)
## 
## Node number 2: 521 observations,    complexity param=0.02058824
##   predicted class=0  expected loss=0.2072937  P(node) =0.6247002
##     class counts:   413   108
##    probabilities: 0.793 0.207 
##   left son=4 (489 obs) right son=5 (32 obs)
##   Primary splits:
##       age      < 9.5      to the right, improve=8.6034020, (0 missing)
##       fare     < 26.26875 to the left,  improve=5.5552180, (0 missing)
##       embarked splits as  LR,           improve=5.4449080, (19 missing)
##       parch    < 0.5      to the left,  improve=2.8740140, (0 missing)
##       sibsp    < 1.5      to the right, improve=0.6491419, (0 missing)
##   Surrogate splits:
##       sibsp < 3.5      to the left,  agree=0.948, adj=0.156, (0 split)
## 
## Node number 3: 313 observations,    complexity param=0.01470588
##   predicted class=1  expected loss=0.2587859  P(node) =0.3752998
##     class counts:    81   232
##    probabilities: 0.259 0.741 
##   left son=6 (219 obs) right son=7 (94 obs)
##   Primary splits:
##       fare     < 48.2     to the left,  improve=13.829810, (0 missing)
##       embarked splits as  LR,           improve= 6.436382, (18 missing)
##       sibsp    < 2.5      to the right, improve= 4.323691, (0 missing)
##       parch    < 3.5      to the right, improve= 3.961923, (0 missing)
##       age      < 30.75    to the left,  improve= 3.160191, (0 missing)
##   Surrogate splits:
##       age < 50.5     to the left,  agree=0.728, adj=0.096, (0 split)
## 
## Node number 4: 489 observations
##   predicted class=0  expected loss=0.1840491  P(node) =0.5863309
##     class counts:   399    90
##    probabilities: 0.816 0.184 
## 
## Node number 5: 32 observations,    complexity param=0.02058824
##   predicted class=1  expected loss=0.4375  P(node) =0.0383693
##     class counts:    14    18
##    probabilities: 0.438 0.563 
##   left son=10 (12 obs) right son=11 (20 obs)
##   Primary splits:
##       sibsp < 2.5      to the right, improve=8.81666700, (0 missing)
##       age   < 3.5      to the right, improve=1.64803900, (0 missing)
##       fare  < 20.825   to the right, improve=0.91017320, (0 missing)
##       parch < 1.5      to the left,  improve=0.07941176, (0 missing)
##   Surrogate splits:
##       fare < 20.825   to the right, agree=0.719, adj=0.250, (0 split)
##       age  < 2.5      to the right, agree=0.656, adj=0.083, (0 split)
## 
## Node number 6: 219 observations,    complexity param=0.01470588
##   predicted class=1  expected loss=0.3561644  P(node) =0.2625899
##     class counts:    78   141
##    probabilities: 0.356 0.644 
##   left son=12 (58 obs) right son=13 (161 obs)
##   Primary splits:
##       fare     < 10.48125 to the left,  improve=6.034415, (0 missing)
##       sibsp    < 2.5      to the right, improve=4.944475, (0 missing)
##       parch    < 3.5      to the right, improve=3.629731, (0 missing)
##       embarked splits as  LR,           improve=2.426902, (16 missing)
##       age      < 48.5     to the left,  improve=1.361004, (0 missing)
## 
## Node number 7: 94 observations
##   predicted class=1  expected loss=0.03191489  P(node) =0.1127098
##     class counts:     3    91
##    probabilities: 0.032 0.968 
## 
## Node number 10: 12 observations
##   predicted class=0  expected loss=0.08333333  P(node) =0.01438849
##     class counts:    11     1
##    probabilities: 0.917 0.083 
## 
## Node number 11: 20 observations
##   predicted class=1  expected loss=0.15  P(node) =0.02398082
##     class counts:     3    17
##    probabilities: 0.150 0.850 
## 
## Node number 12: 58 observations,    complexity param=0.01470588
##   predicted class=0  expected loss=0.4482759  P(node) =0.06954436
##     class counts:    32    26
##    probabilities: 0.552 0.448 
##   left son=24 (43 obs) right son=25 (15 obs)
##   Primary splits:
##       fare  < 7.72915  to the right, improve=5.0059340, (0 missing)
##       age   < 19.5     to the right, improve=2.4843920, (0 missing)
##       sibsp < 0.5      to the right, improve=0.3998001, (0 missing)
##   Surrogate splits:
##       age < 42.5     to the left,  agree=0.759, adj=0.067, (0 split)
## 
## Node number 13: 161 observations,    complexity param=0.01470588
##   predicted class=1  expected loss=0.2857143  P(node) =0.1930456
##     class counts:    46   115
##    probabilities: 0.286 0.714 
##   left son=26 (9 obs) right son=27 (152 obs)
##   Primary splits:
##       sibsp    < 2.5      to the right, improve=6.9365080, (0 missing)
##       parch    < 3.5      to the right, improve=4.7792210, (0 missing)
##       fare     < 27.825   to the right, improve=2.6285710, (0 missing)
##       age      < 18.5     to the left,  improve=0.9387755, (0 missing)
##       embarked splits as  LR,           improve=0.5115292, (2 missing)
##   Surrogate splits:
##       fare < 44.2396  to the right, agree=0.95, adj=0.111, (0 split)
## 
## Node number 24: 43 observations
##   predicted class=0  expected loss=0.3255814  P(node) =0.05155875
##     class counts:    29    14
##    probabilities: 0.674 0.326 
## 
## Node number 25: 15 observations
##   predicted class=1  expected loss=0.2  P(node) =0.01798561
##     class counts:     3    12
##    probabilities: 0.200 0.800 
## 
## Node number 26: 9 observations
##   predicted class=0  expected loss=0.1111111  P(node) =0.01079137
##     class counts:     8     1
##    probabilities: 0.889 0.111 
## 
## Node number 27: 152 observations,    complexity param=0.01470588
##   predicted class=1  expected loss=0.25  P(node) =0.1822542
##     class counts:    38   114
##    probabilities: 0.250 0.750 
##   left son=54 (7 obs) right son=55 (145 obs)
##   Primary splits:
##       parch    < 3.5      to the right, improve=5.4098520, (0 missing)
##       sibsp    < 0.5      to the right, improve=1.7550200, (0 missing)
##       age      < 37       to the right, improve=1.1647510, (0 missing)
##       fare     < 10.50835 to the right, improve=0.9172414, (0 missing)
##       embarked splits as  LR,           improve=0.1858726, (2 missing)
## 
## Node number 54: 7 observations
##   predicted class=0  expected loss=0.1428571  P(node) =0.008393285
##     class counts:     6     1
##    probabilities: 0.857 0.143 
## 
## Node number 55: 145 observations
##   predicted class=1  expected loss=0.2206897  P(node) =0.1738609
##     class counts:    32   113
##    probabilities: 0.221 0.779
#What is the most “important” feature over which the tree first split? 
#Sex
#Do you agree with the Titanic slogan “ Women and children first!”
#from the  variable importance it looks like age and sex have high importance, whcih indicates that they are crucial for survival.
#Use the predict function with your model fit to  make predictions on the test dataset and save it in a variable Prediction:
Prediction <- predict(fit, titanic_testing, type = "class")
Prediction                   
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
##   0   0   1   0   1   0   0   0   1   1   0   0   1   1   1   0   0   1   1   0 
##  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
##   0   0   0   0   0   1   0   0   1   1   1   1   0   1   0   1   0   1   0   0 
##  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
##   1   0   1   0   0   0   1   0   0   0   1   0   1   1   0   1   1   0   0   0 
##  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80 
##   0   0   1   1   1   0   0   0   1   0   0   0   0   1   0   0   0   1   1   1 
##  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 
##   0   0   0   1   0   1   1   1   1   1   0   0   1   1   0   0   0   0   0   0 
## 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 
##   1   0   0   1   0   0   0   1   1   1   0   0   0   1   0   0   0   0   0   0 
## 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 
##   0   0   0   0   0   1   0   0   1   1   0   0   0   0   0   0   0   0   0   0 
## 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 
##   0   0   0   0   1   0   0   0   0   0   1   1   0   0   0   0   0   0   0   0 
## 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 
##   0   1   0   0   1   0   0   0   0   0   0   1   0   0   0   0   0   0   0   0 
## 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 
##   0   0   0   0   0   1   0   0   0   0   0   0   0   1   0   0   1   0   0   0 
## 201 202 203 204 205 206 207 208 209 
##   1   1   0   0   1   0   0   0   0 
## Levels: 0 1
#Save the results in a data frame Results that will have two columns  
#1: PassengerSex =test$Sex, and 2: Survived = Prediction
Results <- data.frame(PassengerSex = titanic_testing$sex,
                      Survived = Prediction)
Results
##     PassengerSex Survived
## 1              1        0
## 2              1        0
## 3              2        1
## 4              1        0
## 5              2        1
## 6              1        0
## 7              1        0
## 8              1        0
## 9              2        1
## 10             2        1
## 11             1        0
## 12             1        0
## 13             2        1
## 14             2        1
## 15             2        1
## 16             1        0
## 17             1        0
## 18             2        1
## 19             2        1
## 20             1        0
## 21             1        0
## 22             1        0
## 23             1        0
## 24             1        0
## 25             1        0
## 26             2        1
## 27             1        0
## 28             1        0
## 29             2        1
## 30             2        1
## 31             2        1
## 32             2        1
## 33             1        0
## 34             2        1
## 35             1        0
## 36             2        1
## 37             1        0
## 38             2        1
## 39             1        0
## 40             1        0
## 41             2        1
## 42             1        0
## 43             2        1
## 44             1        0
## 45             1        0
## 46             1        0
## 47             2        1
## 48             1        0
## 49             1        0
## 50             1        0
## 51             2        1
## 52             1        0
## 53             2        1
## 54             2        1
## 55             1        0
## 56             2        1
## 57             2        1
## 58             1        0
## 59             1        0
## 60             1        0
## 61             1        0
## 62             1        0
## 63             2        1
## 64             2        1
## 65             2        1
## 66             1        0
## 67             1        0
## 68             1        0
## 69             2        1
## 70             1        0
## 71             1        0
## 72             1        0
## 73             1        0
## 74             2        1
## 75             1        0
## 76             1        0
## 77             1        0
## 78             1        1
## 79             2        1
## 80             2        1
## 81             1        0
## 82             1        0
## 83             1        0
## 84             2        1
## 85             1        0
## 86             2        1
## 87             2        1
## 88             2        1
## 89             2        1
## 90             2        1
## 91             1        0
## 92             1        0
## 93             2        1
## 94             1        1
## 95             1        0
## 96             1        0
## 97             1        0
## 98             1        0
## 99             1        0
## 100            2        0
## 101            1        1
## 102            1        0
## 103            1        0
## 104            2        1
## 105            1        0
## 106            1        0
## 107            1        0
## 108            2        1
## 109            2        1
## 110            1        1
## 111            1        0
## 112            1        0
## 113            1        0
## 114            2        1
## 115            1        0
## 116            1        0
## 117            1        0
## 118            2        0
## 119            2        0
## 120            1        0
## 121            1        0
## 122            2        0
## 123            1        0
## 124            1        0
## 125            1        0
## 126            2        1
## 127            1        0
## 128            1        0
## 129            2        1
## 130            2        1
## 131            2        0
## 132            1        0
## 133            1        0
## 134            2        0
## 135            1        0
## 136            1        0
## 137            2        0
## 138            2        0
## 139            1        0
## 140            1        0
## 141            1        0
## 142            2        0
## 143            1        0
## 144            1        0
## 145            1        1
## 146            1        0
## 147            1        0
## 148            1        0
## 149            1        0
## 150            1        0
## 151            2        1
## 152            2        1
## 153            1        0
## 154            1        0
## 155            1        0
## 156            1        0
## 157            1        0
## 158            1        0
## 159            1        0
## 160            1        0
## 161            2        0
## 162            2        1
## 163            1        0
## 164            1        0
## 165            2        1
## 166            1        0
## 167            1        0
## 168            1        0
## 169            1        0
## 170            1        0
## 171            1        0
## 172            2        1
## 173            2        0
## 174            1        0
## 175            2        0
## 176            1        0
## 177            2        0
## 178            1        0
## 179            1        0
## 180            2        0
## 181            2        0
## 182            2        0
## 183            1        0
## 184            2        0
## 185            1        0
## 186            1        1
## 187            1        0
## 188            2        0
## 189            1        0
## 190            1        0
## 191            1        0
## 192            1        0
## 193            1        0
## 194            2        1
## 195            1        0
## 196            1        0
## 197            2        1
## 198            1        0
## 199            1        0
## 200            1        0
## 201            1        1
## 202            2        1
## 203            1        0
## 204            1        0
## 205            2        1
## 206            1        0
## 207            1        0
## 208            1        0
## 209            1        0
#save the data frame as a CSV file
write.csv(Results, file = "Titanicdtree.csv", row.names = FALSE)

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.