library(readr)
train<- read_csv("train.csv")
## Rows: 8693 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): PassengerId, HomePlanet, Cabin, Destination, Name
## dbl (6): Age, RoomService, FoodCourt, ShoppingMall, Spa, VRDeck
## lgl (3): CryoSleep, VIP, Transported
## 
## ℹ 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.
test<- read_csv("test.csv")
## Rows: 4277 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): PassengerId, HomePlanet, Cabin, Destination, Name
## dbl (6): Age, RoomService, FoodCourt, ShoppingMall, Spa, VRDeck
## lgl (2): CryoSleep, VIP
## 
## ℹ 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.
library(rmarkdown)
paged_table(train)
paged_table(test)

-PassengerId - Her yolcu için benzersiz bir Kimlik. Her Kimlik, gggg_pp biçiminde olup, gggg, yolcuyla seyahat eden grubu ve pp, gruptaki sırasını belirtir. Bir gruptaki insanlar genellikle aile üyeleridir, ancak her zaman değil.

Cabin - Yolcunun konakladığı kabin numarası. deck/num/side şeklinde alır, burada side, Port için P veya Starboard için S olabilir . -Destination - Yolcunun ineceği gezegen.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ purrr     1.0.2
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(explore)
train %>% describe_all()
## # A tibble: 14 × 8
##    variable     type     na na_pct unique   min   mean   max
##    <chr>        <chr> <int>  <dbl>  <int> <dbl>  <dbl> <dbl>
##  1 PassengerId  chr       0    0     8693    NA  NA       NA
##  2 HomePlanet   chr     201    2.3      4    NA  NA       NA
##  3 CryoSleep    lgl     217    2.5      3     0   0.36     1
##  4 Cabin        chr     199    2.3   6561    NA  NA       NA
##  5 Destination  chr     182    2.1      4    NA  NA       NA
##  6 Age          dbl     179    2.1     81     0  28.8     79
##  7 VIP          lgl     203    2.3      3     0   0.02     1
##  8 RoomService  dbl     181    2.1   1274     0 225.   14327
##  9 FoodCourt    dbl     183    2.1   1508     0 458.   29813
## 10 ShoppingMall dbl     208    2.4   1116     0 174.   23492
## 11 Spa          dbl     183    2.1   1328     0 311.   22408
## 12 VRDeck       dbl     188    2.2   1307     0 305.   24133
## 13 Name         chr     200    2.3   8474    NA  NA       NA
## 14 Transported  lgl       0    0        2     0   0.5      1
test %>% describe_all()
## # A tibble: 13 × 8
##    variable     type     na na_pct unique   min   mean   max
##    <chr>        <chr> <int>  <dbl>  <int> <dbl>  <dbl> <dbl>
##  1 PassengerId  chr       0    0     4277    NA  NA       NA
##  2 HomePlanet   chr      87    2        4    NA  NA       NA
##  3 CryoSleep    lgl      93    2.2      3     0   0.37     1
##  4 Cabin        chr     100    2.3   3266    NA  NA       NA
##  5 Destination  chr      92    2.2      4    NA  NA       NA
##  6 Age          dbl      91    2.1     80     0  28.7     79
##  7 VIP          lgl      93    2.2      3     0   0.02     1
##  8 RoomService  dbl      82    1.9    843     0 219.   11567
##  9 FoodCourt    dbl     106    2.5    903     0 439.   25273
## 10 ShoppingMall dbl      98    2.3    716     0 177.    8292
## 11 Spa          dbl     101    2.4    834     0 303.   19844
## 12 VRDeck       dbl      80    1.9    797     0 311.   22272
## 13 Name         chr      94    2.2   4177    NA  NA       NA
train[c('group' , 'pp')] <- str_split_fixed(train$PassengerId, '_' , 2)
test[c('group' , 'pp')] <- str_split_fixed(test$PassengerId, '_' , 2)
head(train[, c("PassengerId" , "group" , "pp")])
## # A tibble: 6 × 3
##   PassengerId group pp   
##   <chr>       <chr> <chr>
## 1 0001_01     0001  01   
## 2 0002_01     0002  01   
## 3 0003_01     0003  01   
## 4 0003_02     0003  02   
## 5 0004_01     0004  01   
## 6 0005_01     0005  01
train$withgroup <- ifelse(duplicated(train$group) | duplicated(train$group, fromlast = TRUE),1,0)
test$withgroup <- ifelse(duplicated(test$group) | duplicated(test$group, fromlast = TRUE),1,0)
head(train[, c("PassengerId" , "group" , "pp" , "withgroup")])
## # A tibble: 6 × 4
##   PassengerId group pp    withgroup
##   <chr>       <chr> <chr>     <dbl>
## 1 0001_01     0001  01            0
## 2 0002_01     0002  01            0
## 3 0003_01     0003  01            0
## 4 0003_02     0003  02            1
## 5 0004_01     0004  01            0
## 6 0005_01     0005  01            0
train[c('deck', 'num', 'side')] <- str_split_fixed(train$Cabin, '/', 3)
test[c('deck', 'num', 'side')] <- str_split_fixed(test$Cabin, '/', 3)
train <-train%>% mutate_if(is.character,as.factor)
test <-test%>% mutate_if(is.character,as.factor)
summary(train)
##   PassengerId    HomePlanet   CryoSleep           Cabin     
##  0001_01:   1   Earth :4602   Mode :logical   G/734/S:   8  
##  0002_01:   1   Europa:2131   FALSE:5439      B/11/S :   7  
##  0003_01:   1   Mars  :1759   TRUE :3037      B/201/P:   7  
##  0003_02:   1   NA's  : 201   NA's :217       B/82/S :   7  
##  0004_01:   1                                 C/137/S:   7  
##  0005_01:   1                                 (Other):8458  
##  (Other):8687                                 NA's   : 199  
##         Destination        Age           VIP           RoomService     
##  55 Cancri e  :1800   Min.   : 0.00   Mode :logical   Min.   :    0.0  
##  PSO J318.5-22: 796   1st Qu.:19.00   FALSE:8291      1st Qu.:    0.0  
##  TRAPPIST-1e  :5915   Median :27.00   TRUE :199       Median :    0.0  
##  NA's         : 182   Mean   :28.83   NA's :203       Mean   :  224.7  
##                       3rd Qu.:38.00                   3rd Qu.:   47.0  
##                       Max.   :79.00                   Max.   :14327.0  
##                       NA's   :179                     NA's   :181      
##    FoodCourt        ShoppingMall          Spa              VRDeck       
##  Min.   :    0.0   Min.   :    0.0   Min.   :    0.0   Min.   :    0.0  
##  1st Qu.:    0.0   1st Qu.:    0.0   1st Qu.:    0.0   1st Qu.:    0.0  
##  Median :    0.0   Median :    0.0   Median :    0.0   Median :    0.0  
##  Mean   :  458.1   Mean   :  173.7   Mean   :  311.1   Mean   :  304.9  
##  3rd Qu.:   76.0   3rd Qu.:   27.0   3rd Qu.:   59.0   3rd Qu.:   46.0  
##  Max.   :29813.0   Max.   :23492.0   Max.   :22408.0   Max.   :24133.0  
##  NA's   :183       NA's   :208       NA's   :183       NA's   :188      
##                  Name      Transported         group            pp      
##  Alraium Disivering:   2   Mode :logical   0984   :   8   01     :6217  
##  Ankalik Nateansive:   2   FALSE:4315      4005   :   8   02     :1412  
##  Anton Woody       :   2   TRUE :4378      4256   :   8   03     : 571  
##  Apix Wala         :   2                   4498   :   8   04     : 231  
##  Asch Stradick     :   2                   5133   :   8   05     : 128  
##  (Other)           :8483                   5756   :   8   06     :  75  
##  NA's              : 200                   (Other):8645   (Other):  59  
##    withgroup           deck           num       side    
##  Min.   :0.0000   F      :2794          : 199    : 199  
##  1st Qu.:0.0000   G      :2559   82     :  28   P:4206  
##  Median :0.0000   E      : 876   19     :  22   S:4288  
##  Mean   :0.2848   B      : 779   86     :  22           
##  3rd Qu.:1.0000   C      : 747   176    :  21           
##  Max.   :1.0000   (Other): 739   56     :  21           
##                   NA's   : 199   (Other):8380
train[train == ' '] <- NA
test [test == ' ']<- NA
train$num <- droplevels(train$num)
test$num <- droplevels(test$num)
train$side <- droplevels(train$side)
test$side <- droplevels(test$side)
summary(train)
##   PassengerId    HomePlanet   CryoSleep           Cabin     
##  0001_01:   1   Earth :4602   Mode :logical   G/734/S:   8  
##  0002_01:   1   Europa:2131   FALSE:5439      B/11/S :   7  
##  0003_01:   1   Mars  :1759   TRUE :3037      B/201/P:   7  
##  0003_02:   1   NA's  : 201   NA's :217       B/82/S :   7  
##  0004_01:   1                                 C/137/S:   7  
##  0005_01:   1                                 (Other):8458  
##  (Other):8687                                 NA's   : 199  
##         Destination        Age           VIP           RoomService     
##  55 Cancri e  :1800   Min.   : 0.00   Mode :logical   Min.   :    0.0  
##  PSO J318.5-22: 796   1st Qu.:19.00   FALSE:8291      1st Qu.:    0.0  
##  TRAPPIST-1e  :5915   Median :27.00   TRUE :199       Median :    0.0  
##  NA's         : 182   Mean   :28.83   NA's :203       Mean   :  224.7  
##                       3rd Qu.:38.00                   3rd Qu.:   47.0  
##                       Max.   :79.00                   Max.   :14327.0  
##                       NA's   :179                     NA's   :181      
##    FoodCourt        ShoppingMall          Spa              VRDeck       
##  Min.   :    0.0   Min.   :    0.0   Min.   :    0.0   Min.   :    0.0  
##  1st Qu.:    0.0   1st Qu.:    0.0   1st Qu.:    0.0   1st Qu.:    0.0  
##  Median :    0.0   Median :    0.0   Median :    0.0   Median :    0.0  
##  Mean   :  458.1   Mean   :  173.7   Mean   :  311.1   Mean   :  304.9  
##  3rd Qu.:   76.0   3rd Qu.:   27.0   3rd Qu.:   59.0   3rd Qu.:   46.0  
##  Max.   :29813.0   Max.   :23492.0   Max.   :22408.0   Max.   :24133.0  
##  NA's   :183       NA's   :208       NA's   :183       NA's   :188      
##                  Name      Transported         group            pp      
##  Alraium Disivering:   2   Mode :logical   0984   :   8   01     :6217  
##  Ankalik Nateansive:   2   FALSE:4315      4005   :   8   02     :1412  
##  Anton Woody       :   2   TRUE :4378      4256   :   8   03     : 571  
##  Apix Wala         :   2                   4498   :   8   04     : 231  
##  Asch Stradick     :   2                   5133   :   8   05     : 128  
##  (Other)           :8483                   5756   :   8   06     :  75  
##  NA's              : 200                   (Other):8645   (Other):  59  
##    withgroup           deck           num       side    
##  Min.   :0.0000   F      :2794          : 199    : 199  
##  1st Qu.:0.0000   G      :2559   82     :  28   P:4206  
##  Median :0.0000   E      : 876   19     :  22   S:4288  
##  Mean   :0.2848   B      : 779   86     :  22           
##  3rd Qu.:1.0000   C      : 747   176    :  21           
##  Max.   :1.0000   (Other): 739   56     :  21           
##                   NA's   : 199   (Other):8380
library(zoo)
## 
## Attachement du package : 'zoo'
## Les objets suivants sont masqués depuis 'package:base':
## 
##     as.Date, as.Date.numeric
train <- train %>%
   group_by(group) %>%
    mutate(HomePlanet = na.locf(HomePlanet, na.rm = FALSE))
test <- test %>%
  group_by(group) %>%
    mutate(HomePlanet = na.locf(HomePlanet, na.rm = FALSE))
most_frequent_hp <- train %>%
  filter(!is.na(HomePlanet)) %>%
  group_by(Destination, HomePlanet) %>%
  summarize(count = n()) %>%
  arrange(Destination, desc(count)) %>%
  slice(1) %>%
  ungroup()
## `summarise()` has grouped output by 'Destination'. You can override using the
## `.groups` argument.
most_frequent_hp
## # A tibble: 4 × 3
##   Destination   HomePlanet count
##   <fct>         <fct>      <int>
## 1 55 Cancri e   Europa       893
## 2 PSO J318.5-22 Earth        715
## 3 TRAPPIST-1e   Earth       3116
## 4 <NA>          Earth         99
train$HomePlanet <- as.character(train$HomePlanet)
train$Destination<- as.character(train$Destination)
train <- train %>%
  mutate(HomePlanet = ifelse(is.na(HomePlanet) & Destination == "55 cancri e", "Europa",                                     ifelse(is.na(HomePlanet), "Earth", HomePlanet)))
test$HomePlanet <- as.character(test$HomePlanet)
test$Destination<- as.character(test$Destination)
test <- test %>%
  mutate(HomePlanet = ifelse(is.na(HomePlanet) & Destination == "55 cancri e", "Europa",                                     ifelse(is.na(HomePlanet), "Earth", HomePlanet)))
train %>% describe_all()
## # A tibble: 20 × 8
##    variable     type     na na_pct unique   min   mean   max
##    <chr>        <chr> <int>  <dbl>  <int> <dbl>  <dbl> <dbl>
##  1 PassengerId  fct       0    0     8693    NA  NA       NA
##  2 HomePlanet   chr       4    0        4    NA  NA       NA
##  3 CryoSleep    lgl     217    2.5      3     0   0.36     1
##  4 Cabin        fct     199    2.3   6561    NA  NA       NA
##  5 Destination  chr     182    2.1      4    NA  NA       NA
##  6 Age          dbl     179    2.1     81     0  28.8     79
##  7 VIP          lgl     203    2.3      3     0   0.02     1
##  8 RoomService  dbl     181    2.1   1274     0 225.   14327
##  9 FoodCourt    dbl     183    2.1   1508     0 458.   29813
## 10 ShoppingMall dbl     208    2.4   1116     0 174.   23492
## 11 Spa          dbl     183    2.1   1328     0 311.   22408
## 12 VRDeck       dbl     188    2.2   1307     0 305.   24133
## 13 Name         fct     200    2.3   8474    NA  NA       NA
## 14 Transported  lgl       0    0        2     0   0.5      1
## 15 group        fct       0    0     6217    NA  NA       NA
## 16 pp           fct       0    0        8    NA  NA       NA
## 17 withgroup    dbl       0    0        2     0   0.28     1
## 18 deck         fct     199    2.3      9    NA  NA       NA
## 19 num          fct       0    0     1818    NA  NA       NA
## 20 side         fct       0    0        3    NA  NA       NA
test %>% describe_all()
## # A tibble: 19 × 8
##    variable     type     na na_pct unique   min   mean   max
##    <chr>        <chr> <int>  <dbl>  <int> <dbl>  <dbl> <dbl>
##  1 PassengerId  fct       0    0     4277    NA  NA       NA
##  2 HomePlanet   chr       0    0        3    NA  NA       NA
##  3 CryoSleep    lgl      93    2.2      3     0   0.37     1
##  4 Cabin        fct     100    2.3   3266    NA  NA       NA
##  5 Destination  chr      92    2.2      4    NA  NA       NA
##  6 Age          dbl      91    2.1     80     0  28.7     79
##  7 VIP          lgl      93    2.2      3     0   0.02     1
##  8 RoomService  dbl      82    1.9    843     0 219.   11567
##  9 FoodCourt    dbl     106    2.5    903     0 439.   25273
## 10 ShoppingMall dbl      98    2.3    716     0 177.    8292
## 11 Spa          dbl     101    2.4    834     0 303.   19844
## 12 VRDeck       dbl      80    1.9    797     0 311.   22272
## 13 Name         fct      94    2.2   4177    NA  NA       NA
## 14 group        fct       0    0     3063    NA  NA       NA
## 15 pp           fct       0    0        8    NA  NA       NA
## 16 withgroup    dbl       0    0        2     0   0.28     1
## 17 deck         fct     100    2.3      9    NA  NA       NA
## 18 num          fct       0    0     1506    NA  NA       NA
## 19 side         fct       0    0        3    NA  NA       NA
train <- transform (train, HomePlanet  = replace (HomePlanet, is.na (HomePlanet), "Earth"))
most_frequent_destinations <- train %>%
  filter(!is.na(Destination)) %>%
  group_by(HomePlanet, Destination) %>%
  summarize(count = n()) %>%
  arrange(Destination, desc(count)) %>%
  slice(1) %>%
  ungroup()
## `summarise()` has grouped output by 'HomePlanet'. You can override using the
## `.groups` argument.
most_frequent_destinations
## # A tibble: 3 × 3
##   HomePlanet Destination count
##   <chr>      <chr>       <int>
## 1 Earth      55 Cancri e   713
## 2 Europa     55 Cancri e   893
## 3 Mars       55 Cancri e   194
train<- transform (train, Destination = replace(Destination, is.na(Destination), "TRAPPIST-1e"))
test<- transform (test, Destination = replace(Destination, is.na(Destination), "TRAPPIST-1e"))
train$HomePlanet <- as.factor (train$HomePlanet)
train $destination <- as.factor(train$Destination)
test$HomePlanet <- as.factor (test$HomePlanet)
test$destination <- as.factor(test$Destination)
train <- train %>%
    mutate(RoomService = coalesce (RoomService, 0),
           FoodCourt = coalesce(FoodCourt, 0),
           ShoppingMall = coalesce(ShoppingMall, 0),
           Spa = coalesce(Spa, 0),
           VRDeck = coalesce (VRDeck, 0))
test <- test %>%
    mutate(RoomService = coalesce (RoomService, 0),
           FoodCourt = coalesce(FoodCourt, 0),
           ShoppingMall = coalesce(ShoppingMall, 0),
           Spa = coalesce(Spa, 0),
           VRDeck = coalesce (VRDeck, 0))
train <- train %>%
  group_by(HomePlanet, Destination) %>%
  mutate_at(vars(Age), ~replace_na(., mean (., na.rm =TRUE)))
test <- test %>%
  group_by(HomePlanet, Destination) %>%
  mutate_at(vars(Age), ~replace_na(., mean (., na.rm =TRUE)))
train$expense <- train$RoomService + train$FoodCourt + train$ShoppingMall + train$Spa + train$VRDeck
test$expense <- test$RoomService + test$FoodCourt + test$ShoppingMall + test$Spa + test$VRDeck
train <- transform(train, CryoSleep = replace(CryoSleep,is.na(CryoSleep) & expense>0 & Age>12, "FALSE"))
test <- transform(test, CryoSleep = replace(CryoSleep,is.na(CryoSleep) & expense>0 & Age>12, "FALSE"))
summary(train)
##   PassengerId    HomePlanet    CryoSleep             Cabin     
##  0001_01:   1   Earth :4764   Length:8693        G/734/S:   8  
##  0002_01:   1   Europa:2153   Class :character   B/11/S :   7  
##  0003_01:   1   Mars  :1776   Mode  :character   B/201/P:   7  
##  0003_02:   1                                    B/82/S :   7  
##  0004_01:   1                                    C/137/S:   7  
##  0005_01:   1                                    (Other):8458  
##  (Other):8687                                    NA's   : 199  
##  Destination             Age           VIP           RoomService   
##  Length:8693        Min.   : 0.00   Mode :logical   Min.   :    0  
##  Class :character   1st Qu.:20.00   FALSE:8291      1st Qu.:    0  
##  Mode  :character   Median :27.00   TRUE :199       Median :    0  
##                     Mean   :28.84   NA's :203       Mean   :  220  
##                     3rd Qu.:37.00                   3rd Qu.:   41  
##                     Max.   :79.00                   Max.   :14327  
##                                                                    
##    FoodCourt        ShoppingMall          Spa              VRDeck       
##  Min.   :    0.0   Min.   :    0.0   Min.   :    0.0   Min.   :    0.0  
##  1st Qu.:    0.0   1st Qu.:    0.0   1st Qu.:    0.0   1st Qu.:    0.0  
##  Median :    0.0   Median :    0.0   Median :    0.0   Median :    0.0  
##  Mean   :  448.4   Mean   :  169.6   Mean   :  304.6   Mean   :  298.3  
##  3rd Qu.:   61.0   3rd Qu.:   22.0   3rd Qu.:   53.0   3rd Qu.:   40.0  
##  Max.   :29813.0   Max.   :23492.0   Max.   :22408.0   Max.   :24133.0  
##                                                                         
##                  Name      Transported         group            pp      
##  Alraium Disivering:   2   Mode :logical   0984   :   8   01     :6217  
##  Ankalik Nateansive:   2   FALSE:4315      4005   :   8   02     :1412  
##  Anton Woody       :   2   TRUE :4378      4256   :   8   03     : 571  
##  Apix Wala         :   2                   4498   :   8   04     : 231  
##  Asch Stradick     :   2                   5133   :   8   05     : 128  
##  (Other)           :8483                   5756   :   8   06     :  75  
##  NA's              : 200                   (Other):8645   (Other):  59  
##    withgroup           deck           num       side            destination  
##  Min.   :0.0000   F      :2794          : 199    : 199   55 Cancri e  :1800  
##  1st Qu.:0.0000   G      :2559   82     :  28   P:4206   PSO J318.5-22: 796  
##  Median :0.0000   E      : 876   19     :  22   S:4288   TRAPPIST-1e  :6097  
##  Mean   :0.2848   B      : 779   86     :  22                                
##  3rd Qu.:1.0000   C      : 747   176    :  21                                
##  Max.   :1.0000   (Other): 739   56     :  21                                
##                   NA's   : 199   (Other):8380                                
##     expense     
##  Min.   :    0  
##  1st Qu.:    0  
##  Median :  716  
##  Mean   : 1441  
##  3rd Qu.: 1441  
##  Max.   :35987  
## 
train <- transform(train, CryoSleep = replace(CryoSleep, is.na(CryoSleep) & expense==0 & Age>12, "TRUE"))
test <- transform(test, CryoSleep = replace(CryoSleep,is.na(CryoSleep) & expense==0 & Age>12, "TRUE"))
describe_all (train)
## # A tibble: 22 × 8
##    variable     type     na na_pct unique   min   mean   max
##    <chr>        <chr> <int>  <dbl>  <int> <dbl>  <dbl> <dbl>
##  1 PassengerId  fct       0    0     8693    NA  NA       NA
##  2 HomePlanet   fct       0    0        3    NA  NA       NA
##  3 CryoSleep    chr      23    0.3      3    NA  NA       NA
##  4 Cabin        fct     199    2.3   6561    NA  NA       NA
##  5 Destination  chr       0    0        3    NA  NA       NA
##  6 Age          dbl       0    0       88     0  28.8     79
##  7 VIP          lgl     203    2.3      3     0   0.02     1
##  8 RoomService  dbl       0    0     1273     0 220.   14327
##  9 FoodCourt    dbl       0    0     1507     0 448.   29813
## 10 ShoppingMall dbl       0    0     1115     0 170.   23492
## # ℹ 12 more rows
train$CryoSleep <- as.factor(train$CryoSleep)
test$CryoSleep <- as.factor(test$CryoSleep)
summary(train)
##   PassengerId    HomePlanet   CryoSleep        Cabin      Destination       
##  0001_01:   1   Earth :4764   FALSE:5558   G/734/S:   8   Length:8693       
##  0002_01:   1   Europa:2153   TRUE :3112   B/11/S :   7   Class :character  
##  0003_01:   1   Mars  :1776   NA's :  23   B/201/P:   7   Mode  :character  
##  0003_02:   1                              B/82/S :   7                     
##  0004_01:   1                              C/137/S:   7                     
##  0005_01:   1                              (Other):8458                     
##  (Other):8687                              NA's   : 199                     
##       Age           VIP           RoomService      FoodCourt      
##  Min.   : 0.00   Mode :logical   Min.   :    0   Min.   :    0.0  
##  1st Qu.:20.00   FALSE:8291      1st Qu.:    0   1st Qu.:    0.0  
##  Median :27.00   TRUE :199       Median :    0   Median :    0.0  
##  Mean   :28.84   NA's :203       Mean   :  220   Mean   :  448.4  
##  3rd Qu.:37.00                   3rd Qu.:   41   3rd Qu.:   61.0  
##  Max.   :79.00                   Max.   :14327   Max.   :29813.0  
##                                                                   
##   ShoppingMall          Spa              VRDeck       
##  Min.   :    0.0   Min.   :    0.0   Min.   :    0.0  
##  1st Qu.:    0.0   1st Qu.:    0.0   1st Qu.:    0.0  
##  Median :    0.0   Median :    0.0   Median :    0.0  
##  Mean   :  169.6   Mean   :  304.6   Mean   :  298.3  
##  3rd Qu.:   22.0   3rd Qu.:   53.0   3rd Qu.:   40.0  
##  Max.   :23492.0   Max.   :22408.0   Max.   :24133.0  
##                                                       
##                  Name      Transported         group            pp      
##  Alraium Disivering:   2   Mode :logical   0984   :   8   01     :6217  
##  Ankalik Nateansive:   2   FALSE:4315      4005   :   8   02     :1412  
##  Anton Woody       :   2   TRUE :4378      4256   :   8   03     : 571  
##  Apix Wala         :   2                   4498   :   8   04     : 231  
##  Asch Stradick     :   2                   5133   :   8   05     : 128  
##  (Other)           :8483                   5756   :   8   06     :  75  
##  NA's              : 200                   (Other):8645   (Other):  59  
##    withgroup           deck           num       side            destination  
##  Min.   :0.0000   F      :2794          : 199    : 199   55 Cancri e  :1800  
##  1st Qu.:0.0000   G      :2559   82     :  28   P:4206   PSO J318.5-22: 796  
##  Median :0.0000   E      : 876   19     :  22   S:4288   TRAPPIST-1e  :6097  
##  Mean   :0.2848   B      : 779   86     :  22                                
##  3rd Qu.:1.0000   C      : 747   176    :  21                                
##  Max.   :1.0000   (Other): 739   56     :  21                                
##                   NA's   : 199   (Other):8380                                
##     expense     
##  Min.   :    0  
##  1st Qu.:    0  
##  Median :  716  
##  Mean   : 1441  
##  3rd Qu.: 1441  
##  Max.   :35987  
## 
train <- transform(train, VIP = replace(VIP , is.na (VIP), FALSE))
test <- transform(test, VIP = replace(VIP , is.na (VIP), FALSE))
train %>% describe_all()
## # A tibble: 22 × 8
##    variable     type     na na_pct unique   min   mean   max
##    <chr>        <chr> <int>  <dbl>  <int> <dbl>  <dbl> <dbl>
##  1 PassengerId  fct       0    0     8693    NA  NA       NA
##  2 HomePlanet   fct       0    0        3    NA  NA       NA
##  3 CryoSleep    fct      23    0.3      3    NA  NA       NA
##  4 Cabin        fct     199    2.3   6561    NA  NA       NA
##  5 Destination  chr       0    0        3    NA  NA       NA
##  6 Age          dbl       0    0       88     0  28.8     79
##  7 VIP          lgl       0    0        2     0   0.02     1
##  8 RoomService  dbl       0    0     1273     0 220.   14327
##  9 FoodCourt    dbl       0    0     1507     0 448.   29813
## 10 ShoppingMall dbl       0    0     1115     0 170.   23492
## # ℹ 12 more rows
train <- train %>%
   group_by (group) %>%
   mutate(deck = na.locf (deck, na.rm = FALSE))
test <- test %>%
   group_by (group) %>%
   mutate(deck = na.locf (deck, na.rm = FALSE))
summary(train)
##   PassengerId    HomePlanet   CryoSleep        Cabin      Destination       
##  0001_01:   1   Earth :4764   FALSE:5558   G/734/S:   8   Length:8693       
##  0002_01:   1   Europa:2153   TRUE :3112   B/11/S :   7   Class :character  
##  0003_01:   1   Mars  :1776   NA's :  23   B/201/P:   7   Mode  :character  
##  0003_02:   1                              B/82/S :   7                     
##  0004_01:   1                              C/137/S:   7                     
##  0005_01:   1                              (Other):8458                     
##  (Other):8687                              NA's   : 199                     
##       Age           VIP           RoomService      FoodCourt      
##  Min.   : 0.00   Mode :logical   Min.   :    0   Min.   :    0.0  
##  1st Qu.:20.00   FALSE:8494      1st Qu.:    0   1st Qu.:    0.0  
##  Median :27.00   TRUE :199       Median :    0   Median :    0.0  
##  Mean   :28.84                   Mean   :  220   Mean   :  448.4  
##  3rd Qu.:37.00                   3rd Qu.:   41   3rd Qu.:   61.0  
##  Max.   :79.00                   Max.   :14327   Max.   :29813.0  
##                                                                   
##   ShoppingMall          Spa              VRDeck       
##  Min.   :    0.0   Min.   :    0.0   Min.   :    0.0  
##  1st Qu.:    0.0   1st Qu.:    0.0   1st Qu.:    0.0  
##  Median :    0.0   Median :    0.0   Median :    0.0  
##  Mean   :  169.6   Mean   :  304.6   Mean   :  298.3  
##  3rd Qu.:   22.0   3rd Qu.:   53.0   3rd Qu.:   40.0  
##  Max.   :23492.0   Max.   :22408.0   Max.   :24133.0  
##                                                       
##                  Name      Transported         group            pp      
##  Alraium Disivering:   2   Mode :logical   0984   :   8   01     :6217  
##  Ankalik Nateansive:   2   FALSE:4315      4005   :   8   02     :1412  
##  Anton Woody       :   2   TRUE :4378      4256   :   8   03     : 571  
##  Apix Wala         :   2                   4498   :   8   04     : 231  
##  Asch Stradick     :   2                   5133   :   8   05     : 128  
##  (Other)           :8483                   5756   :   8   06     :  75  
##  NA's              : 200                   (Other):8645   (Other):  59  
##    withgroup           deck           num       side            destination  
##  Min.   :0.0000   F      :2801          : 199    : 199   55 Cancri e  :1800  
##  1st Qu.:0.0000   G      :2580   82     :  28   P:4206   PSO J318.5-22: 796  
##  Median :0.0000   E      : 881   19     :  22   S:4288   TRAPPIST-1e  :6097  
##  Mean   :0.2848   B      : 790   86     :  22                                
##  3rd Qu.:1.0000   C      : 758   176    :  21                                
##  Max.   :1.0000   (Other): 749   56     :  21                                
##                   NA's   : 134   (Other):8380                                
##     expense     
##  Min.   :    0  
##  1st Qu.:    0  
##  Median :  716  
##  Mean   : 1441  
##  3rd Qu.: 1441  
##  Max.   :35987  
## 
most_frequent_deck <- train %>%
  filter(!is.na(deck)) %>%
  group_by(HomePlanet,  deck) %>%
  summarize (count = n()) %>%
  arrange(HomePlanet, desc(count)) %>%
  slice (1) %>%
  ungroup ()
## `summarise()` has grouped output by 'HomePlanet'. You can override using the
## `.groups` argument.
most_frequent_deck
## # A tibble: 3 × 3
##   HomePlanet deck  count
##   <fct>      <fct> <int>
## 1 Earth      G      2580
## 2 Europa     B       786
## 3 Mars       F      1122
train$HomePlanet  <- as.character(train$HomePlanet)
train$deck <- as.character(train$deck)
train <- train %>%
  mutate(deck = ifelse(is.na(deck) & HomePlanet == "Earth", "G",
                       ifelse(is.na(deck) & HomePlanet == "Europa", "B" ,
                              ifelse(is.na(deck) & HomePlanet == "Mars", "F", deck))))
most_frequent_side <- train %>%
  filter(!is.na(deck)) %>%
  group_by(HomePlanet,  side) %>%
  summarize (count = n()) %>%
  arrange(HomePlanet, desc(count)) %>%
  slice (1) %>%
  ungroup ()
## `summarise()` has grouped output by 'HomePlanet'. You can override using the
## `.groups` argument.
most_frequent_side
## # A tibble: 3 × 3
##   HomePlanet side  count
##   <chr>      <fct> <int>
## 1 Earth      P      2349
## 2 Europa     S      1131
## 3 Mars       P       897
train$side <- as.character(train$side)
train <- train %>%
  mutate(side = ifelse(is.na(side) & HomePlanet == "Earth", "P",
                       ifelse(is.na(side) & HomePlanet == "Europa", "S" ,
                              ifelse(is.na(side) & HomePlanet == "Mars", "P", side))))
test$side <- as.character(test$side)
test <- test %>%
  mutate(side = ifelse(is.na(side) & HomePlanet == "Earth", "P",
                       ifelse(is.na(side) & HomePlanet == "Europa", "S" ,
                              ifelse(is.na(side) & HomePlanet == "Mars", "P", side))))
train %>% describe_all()
## # A tibble: 22 × 8
##    variable     type     na na_pct unique   min   mean   max
##    <chr>        <chr> <int>  <dbl>  <int> <dbl>  <dbl> <dbl>
##  1 PassengerId  fct       0    0     8693    NA  NA       NA
##  2 HomePlanet   chr       0    0        3    NA  NA       NA
##  3 CryoSleep    fct      23    0.3      3    NA  NA       NA
##  4 Cabin        fct     199    2.3   6561    NA  NA       NA
##  5 Destination  chr       0    0        3    NA  NA       NA
##  6 Age          dbl       0    0       88     0  28.8     79
##  7 VIP          lgl       0    0        2     0   0.02     1
##  8 RoomService  dbl       0    0     1273     0 220.   14327
##  9 FoodCourt    dbl       0    0     1507     0 448.   29813
## 10 ShoppingMall dbl       0    0     1115     0 170.   23492
## # ℹ 12 more rows
train <- train %>% select(-c("Cabin", "Name","group","pp","num"))
## Adding missing grouping variables: `group`
test <- test %>% select(-c("Cabin", "Name","group","pp","num"))
## Adding missing grouping variables: `group`
describe_all(train)
## # A tibble: 18 × 8
##    variable     type     na na_pct unique   min    mean   max
##    <chr>        <chr> <int>  <dbl>  <int> <dbl>   <dbl> <dbl>
##  1 group        fct       0    0     6217    NA   NA       NA
##  2 PassengerId  fct       0    0     8693    NA   NA       NA
##  3 HomePlanet   chr       0    0        3    NA   NA       NA
##  4 CryoSleep    fct      23    0.3      3    NA   NA       NA
##  5 Destination  chr       0    0        3    NA   NA       NA
##  6 Age          dbl       0    0       88     0   28.8     79
##  7 VIP          lgl       0    0        2     0    0.02     1
##  8 RoomService  dbl       0    0     1273     0  220.   14327
##  9 FoodCourt    dbl       0    0     1507     0  448.   29813
## 10 ShoppingMall dbl       0    0     1115     0  170.   23492
## 11 Spa          dbl       0    0     1327     0  305.   22408
## 12 VRDeck       dbl       0    0     1306     0  298.   24133
## 13 Transported  lgl       0    0        2     0    0.5      1
## 14 withgroup    dbl       0    0        2     0    0.28     1
## 15 deck         chr       0    0        8    NA   NA       NA
## 16 side         chr       0    0        3    NA   NA       NA
## 17 destination  fct       0    0        3    NA   NA       NA
## 18 expense      dbl       0    0     2336     0 1441.   35987
train <- train %>% mutate_if(is.character,as.factor)
## `mutate_if()` ignored the following grouping variables:
## • Column `group`
test <- test %>% mutate_if(is.character,as.factor)
## `mutate_if()` ignored the following grouping variables:
## • Column `group`
train$PassengerId <- as.character(train$PassengerId)
test$PassengerId <- as.character(test$PassengerId)
train$group <- NULL
test$group <- NULL
write.csv(train, "train_c.csv", row.names=FALSE)
write.csv(test, "test_c.csv", row.names=FALSE)