Basic Data Loading and Transformation

Author

Mohd Afwan Shaikh

Approach

For this assignment, I plan to use the Titanic passenger dataset to practice basic data loading and transformation in R. I chose the Titanic dataset because I liked the movie and became interested in learning more about the passengers, especially the survivors and the people who died. I will load the dataset from Github (Titanic.csv, https://github.com/YBI-Foundation), select useful columns such as survival status, passenger class, sex, age, and fare, and make the data easier to understand by renaming or transforming values where needed.

Some challenges I expect are missing values, especially in the age column, and understanding how the variables are represented in the original dataset.

Loading the Data

The Titanic dataset is loaded directly from my GitHub repository so that the data can be accessed online and the analysis is reproducible.

titanic <- read.csv(
  "https://raw.githubusercontent.com/Afwans/DATA607-Assignment1/main/Titanic.csv"
)

head(titanic)
  pclass survived                                            name    sex   age
1      1        1                   Allen, Miss. Elisabeth Walton female 29.00
2      1        1                  Allison, Master. Hudson Trevor   male  0.92
3      1        0                    Allison, Miss. Helen Loraine female  2.00
4      1        0            Allison, Mr. Hudson Joshua Creighton   male 30.00
5      1        0 Allison, Mrs. Hudson J C (Bessie Waldo Daniels) female 25.00
6      1        1                             Anderson, Mr. Harry   male 48.00
  sibsp parch ticket     fare   cabin embarked boat body
1     0     0  24160 211.3375      B5        S    2   NA
2     1     2 113781 151.5500 C22 C26        S   11   NA
3     1     2 113781 151.5500 C22 C26        S        NA
4     1     2 113781 151.5500 C22 C26        S       135
5     1     2 113781 151.5500 C22 C26        S        NA
6     0     0  19952  26.5500     E12        S    3   NA
                        home.dest
1                    St Louis, MO
2 Montreal, PQ / Chesterville, ON
3 Montreal, PQ / Chesterville, ON
4 Montreal, PQ / Chesterville, ON
5 Montreal, PQ / Chesterville, ON
6                    New York, NY

Inspecting the Data

Before transforming the dataset, I reviewed the column names and structure of the data to understand the available variables.

names(titanic)
 [1] "pclass"    "survived"  "name"      "sex"       "age"       "sibsp"    
 [7] "parch"     "ticket"    "fare"      "cabin"     "embarked"  "boat"     
[13] "body"      "home.dest"
str(titanic)
'data.frame':   1309 obs. of  14 variables:
 $ pclass   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ survived : int  1 1 0 0 0 1 1 0 1 0 ...
 $ name     : chr  "Allen, Miss. Elisabeth Walton" "Allison, Master. Hudson Trevor" "Allison, Miss. Helen Loraine" "Allison, Mr. Hudson Joshua Creighton" ...
 $ sex      : chr  "female" "male" "female" "male" ...
 $ age      : num  29 0.92 2 30 25 48 63 39 53 71 ...
 $ sibsp    : int  0 1 1 1 1 0 1 0 2 0 ...
 $ parch    : int  0 2 2 2 2 0 0 0 0 0 ...
 $ ticket   : chr  "24160" "113781" "113781" "113781" ...
 $ fare     : num  211 152 152 152 152 ...
 $ cabin    : chr  "B5" "C22 C26" "C22 C26" "C22 C26" ...
 $ embarked : chr  "S" "S" "S" "S" ...
 $ boat     : chr  "2" "11" "" "" ...
 $ body     : int  NA NA NA 135 NA NA NA NA NA 22 ...
 $ home.dest: chr  "St Louis, MO" "Montreal, PQ / Chesterville, ON" "Montreal, PQ / Chesterville, ON" "Montreal, PQ / Chesterville, ON" ...
summary(titanic)
     pclass         survived            name             sex      
 Min.   :1.000   Min.   :0.000   Length   :1309   Length   :1309  
 1st Qu.:2.000   1st Qu.:0.000   N.unique :1307   N.unique :   2  
 Median :3.000   Median :0.000   N.blank  :   0   N.blank  :   0  
 Mean   :2.295   Mean   :0.382   Min.nchar:  12   Min.nchar:   4  
 3rd Qu.:3.000   3rd Qu.:1.000   Max.nchar:  82   Max.nchar:   6  
 Max.   :3.000   Max.   :1.000                                    
                                                                  
      age            sibsp            parch             ticket    
 Min.   : 0.17   Min.   :0.0000   Min.   :0.000   Length   :1309  
 1st Qu.:21.00   1st Qu.:0.0000   1st Qu.:0.000   N.unique : 929  
 Median :28.00   Median :0.0000   Median :0.000   N.blank  :   0  
 Mean   :29.88   Mean   :0.4989   Mean   :0.385   Min.nchar:   3  
 3rd Qu.:39.00   3rd Qu.:1.0000   3rd Qu.:0.000   Max.nchar:  18  
 Max.   :80.00   Max.   :8.0000   Max.   :9.000                   
 NAs    :263                                                      
      fare               cabin           embarked           boat     
 Min.   :  0.000   Length   :1309   Length   :1309   Length   :1309  
 1st Qu.:  7.896   N.unique : 187   N.unique :   4   N.unique :  28  
 Median : 14.454   N.blank  :1014   N.blank  :   2   N.blank  : 823  
 Mean   : 33.295   Min.nchar:   0   Min.nchar:   0   Min.nchar:   0  
 3rd Qu.: 31.275   Max.nchar:  15   Max.nchar:   1   Max.nchar:   7  
 Max.   :512.329                                                     
 NAs    :1                                                           
      body           home.dest   
 Min.   :  1.0   Length   :1309  
 1st Qu.: 72.0   N.unique : 370  
 Median :155.0   N.blank  : 564  
 Mean   :160.8   Min.nchar:   0  
 3rd Qu.:256.0   Max.nchar:  50  
 Max.   :328.0                   
 NAs    :1188                    

Selecting Relevant Columns

For this analysis, I selected variables that describe the passenger and may help explain survival. These include survival status, passenger class, sex, age, fare, and cabin.

titanic_subset <- titanic[
  , c("survived", "pclass", "sex", "age", "fare", "cabin")
]

head(titanic_subset)
  survived pclass    sex   age     fare   cabin
1        1      1 female 29.00 211.3375      B5
2        1      1   male  0.92 151.5500 C22 C26
3        0      1 female  2.00 151.5500 C22 C26
4        0      1   male 30.00 151.5500 C22 C26
5        0      1 female 25.00 151.5500 C22 C26
6        1      1   male 48.00  26.5500     E12

Transforming Values

The original dataset uses numeric codes for survival status and passenger class. I transformed these values into descriptive labels so the data is easier to understand.

titanic_subset <- titanic[
  , c("survived", "pclass", "sex", "age", "fare", "cabin")
]

titanic_subset$survived <- ifelse(
  titanic_subset$survived == 1,
  "Survived",
  "Did Not Survive"
)

titanic_subset$pclass <- ifelse(
  titanic_subset$pclass == 1, "First Class",
  ifelse(
    titanic_subset$pclass == 2,
    "Second Class",
    "Third Class"
  )
)

head(titanic_subset)
         survived      pclass    sex   age     fare   cabin
1        Survived First Class female 29.00 211.3375      B5
2        Survived First Class   male  0.92 151.5500 C22 C26
3 Did Not Survive First Class female  2.00 151.5500 C22 C26
4 Did Not Survive First Class   male 30.00 151.5500 C22 C26
5 Did Not Survive First Class female 25.00 151.5500 C22 C26
6        Survived First Class   male 48.00  26.5500     E12

Checking Missing Values

I checked the selected columns for missing values to understand whether any important passenger information was incomplete.

colSums(is.na(titanic_subset))
survived   pclass      sex      age     fare    cabin 
       0        0        0      263        1        0 
sum(titanic_subset$cabin == "")
[1] 1014

Survival Summary

I summarized the survival status to compare the number of passengers who survived with the number who did not survive.

table(titanic_subset$survived)

Did Not Survive        Survived 
            809             500 
table(
  titanic_subset$pclass,
  titanic_subset$survived
)
              
               Did Not Survive Survived
  First Class              123      200
  Second Class             158      119
  Third Class              528      181

Conclusions and Recommendations

The Titanic dataset contained 1,309 passengers. After reviewing the data, I selected survival status, passenger class, sex, age, fare, and cabin for further analysis. I also transformed the survival and passenger class codes into descriptive values to make the dataset easier to understand.

The results showed that 500 passengers survived and 809 did not survive. Passenger class also appeared to be related to survival. In first class, 200 passengers survived compared with 123 who did not survive. Third class had the largest number of passengers who did not survive, with 528 deaths compared with 181 survivors.

The dataset also had missing information. There were 263 missing age values, one missing fare value, and 1,014 blank cabin values. In future analysis, I would examine how sex, age, passenger class, and cabin location were related to survival and determine how the missing values should be handled.