This assignment is about importing data using different sources, from csv, statistical packages which is sav, text files(xml), database which I used postgres database.
#importing using csv file, the dataset is fish which it contains measurements of different fish species.
data_csv <- read.csv("D:/Personal/Masters/R_programming/Fina_assignment/Fish.csv")
# Getting summary of the dataset
summary(data_csv)
## Species Weight Length1 Length2
## Length :159 Min. : 0.0 Min. : 7.50 Min. : 8.40
## N.unique : 7 1st Qu.: 120.0 1st Qu.:19.05 1st Qu.:21.00
## N.blank : 0 Median : 273.0 Median :25.20 Median :27.30
## Min.nchar: 4 Mean : 398.3 Mean :26.25 Mean :28.42
## Max.nchar: 9 3rd Qu.: 650.0 3rd Qu.:32.70 3rd Qu.:35.50
## Max. :1650.0 Max. :59.00 Max. :63.40
## Length3 Height Width
## Min. : 8.80 Min. : 1.728 Min. :1.048
## 1st Qu.:23.15 1st Qu.: 5.945 1st Qu.:3.386
## Median :29.40 Median : 7.786 Median :4.248
## Mean :31.23 Mean : 8.971 Mean :4.417
## 3rd Qu.:39.65 3rd Qu.:12.366 3rd Qu.:5.585
## Max. :68.00 Max. :18.957 Max. :8.142
#Displaying the structure of a dataset
str(data_csv)
## 'data.frame': 159 obs. of 7 variables:
## $ Species: chr "Bream" "Bream" "Bream" "Bream" ...
## $ Weight : num 242 290 340 363 430 450 500 390 450 500 ...
## $ Length1: num 23.2 24 23.9 26.3 26.5 26.8 26.8 27.6 27.6 28.5 ...
## $ Length2: num 25.4 26.3 26.5 29 29 29.7 29.7 30 30 30.7 ...
## $ Length3: num 30 31.2 31.1 33.5 34 34.7 34.5 35 35.1 36.2 ...
## $ Height : num 11.5 12.5 12.4 12.7 12.4 ...
## $ Width : num 4.02 4.31 4.7 4.46 5.13 ...
# Getting the first 6 rows of the dataset
head(data_csv)
## Species Weight Length1 Length2 Length3 Height Width
## 1 Bream 242 23.2 25.4 30.0 11.5200 4.0200
## 2 Bream 290 24.0 26.3 31.2 12.4800 4.3056
## 3 Bream 340 23.9 26.5 31.1 12.3778 4.6961
## 4 Bream 363 26.3 29.0 33.5 12.7300 4.4555
## 5 Bream 430 26.5 29.0 34.0 12.4440 5.1340
## 6 Bream 450 26.8 29.7 34.7 13.6024 4.9274
# Getting the last 6 rows of the dataset
tail(data_csv)
## Species Weight Length1 Length2 Length3 Height Width
## 154 Smelt 9.8 11.4 12.0 13.2 2.2044 1.1484
## 155 Smelt 12.2 11.5 12.2 13.4 2.0904 1.3936
## 156 Smelt 13.4 11.7 12.4 13.5 2.4300 1.2690
## 157 Smelt 12.2 12.1 13.0 13.8 2.2770 1.2558
## 158 Smelt 19.7 13.2 14.3 15.2 2.8728 2.0672
## 159 Smelt 19.9 13.8 15.0 16.2 2.9322 1.8792
# The sleep is a statistical dataset, about sleep habits, sleep quality, health, and lifestyle factors of people.
library(haven)
data_sav <- read_sav("D:/Personal/Masters/R_programming/Fina_assignment/sleep.sav")
# Getting the first 6 rows of the dataset
# Getting summary of the dataset
summary(data_sav)
## id sex age marital edlevel
## Min. : 1.0 Min. :0.0000 Min. :18.00 Min. :1.000 Min. :1.0
## 1st Qu.: 68.5 1st Qu.:0.0000 1st Qu.:34.00 1st Qu.:2.000 1st Qu.:4.0
## Median :237.0 Median :0.0000 Median :44.00 Median :2.000 Median :4.0
## Mean :236.9 Mean :0.4465 Mean :43.87 Mean :1.937 Mean :4.1
## 3rd Qu.:401.5 3rd Qu.:1.0000 3rd Qu.:54.00 3rd Qu.:2.000 3rd Qu.:5.0
## Max. :546.0 Max. :1.0000 Max. :84.00 Max. :4.000 Max. :5.0
## NAs :23 NAs :2
## weight height healthrate fitrate
## Min. : 43.00 Min. :150.0 Min. : 3.000 Min. : 1.000
## 1st Qu.: 63.00 1st Qu.:162.5 1st Qu.: 7.000 1st Qu.: 5.000
## Median : 72.00 Median :170.0 Median : 8.000 Median : 7.000
## Mean : 73.38 Mean :170.2 Mean : 7.794 Mean : 6.421
## 3rd Qu.: 82.00 3rd Qu.:178.0 3rd Qu.: 9.000 3rd Qu.: 8.000
## Max. :160.00 Max. :199.0 Max. :10.000 Max. :10.000
## NAs :22 NAs :25 NAs :4 NAs :5
## weightrate smoke smokenum alchohol
## Min. : 2.000 Min. :1.000 Min. : 0.0 Min. : 0.000
## 1st Qu.: 5.000 1st Qu.:2.000 1st Qu.: 5.0 1st Qu.: 0.000
## Median : 6.000 Median :2.000 Median :10.0 Median : 1.000
## Mean : 6.308 Mean :1.874 Mean :13.3 Mean : 1.002
## 3rd Qu.: 7.000 3rd Qu.:2.000 3rd Qu.:19.0 3rd Qu.: 1.500
## Max. :10.000 Max. :2.000 Max. :78.0 Max. :12.000
## NAs :5 NAs :1 NAs :236 NAs :14
## caffeine hourwnit hourwend hourneed
## Min. : 0.000 Min. : 3.000 Min. : 3.000 Min. : 2.000
## 1st Qu.: 2.000 1st Qu.: 6.000 1st Qu.: 7.000 1st Qu.: 7.000
## Median : 3.000 Median : 7.000 Median : 8.000 Median : 8.000
## Mean : 2.943 Mean : 6.972 Mean : 7.807 Mean : 7.754
## 3rd Qu.: 4.000 3rd Qu.: 8.000 3rd Qu.: 8.500 3rd Qu.: 8.000
## Max. :10.000 Max. :10.000 Max. :14.000 Max. :16.000
## NAs :9 NAs :1 NAs :2 NAs :5
## trubslep trubstay wakenite niteshft liteslp
## Min. :1.000 Min. :1.000 Min. :1.000 Min. :1.000 Min. :1.00
## 1st Qu.:1.000 1st Qu.:1.000 1st Qu.:1.000 1st Qu.:2.000 1st Qu.:1.00
## Median :2.000 Median :2.000 Median :1.000 Median :2.000 Median :2.00
## Mean :1.606 Mean :1.564 Mean :1.203 Mean :1.963 Mean :1.56
## 3rd Qu.:2.000 3rd Qu.:2.000 3rd Qu.:1.000 3rd Qu.:2.000 3rd Qu.:2.00
## Max. :2.000 Max. :2.000 Max. :2.000 Max. :3.000 Max. :2.00
## NAs :2 NAs :5 NAs :2 NAs :5
## refreshd satsleep qualslp stressmo
## Min. :1.000 Min. : 1.000 Min. :1.000 Min. : 1.000
## 1st Qu.:1.000 1st Qu.: 4.000 1st Qu.:3.000 1st Qu.: 4.000
## Median :2.000 Median : 5.000 Median :4.000 Median : 6.000
## Mean :1.621 Mean : 5.549 Mean :3.698 Mean : 5.696
## 3rd Qu.:2.000 3rd Qu.: 8.000 3rd Qu.:4.250 3rd Qu.: 8.000
## Max. :2.000 Max. :10.000 Max. :6.000 Max. :10.000
## NAs :7 NAs :3 NAs :3
## medhelp problem impact1 impact2
## Min. :1.000 Min. :1.000 Min. : 1.000 Min. : 1.000
## 1st Qu.:2.000 1st Qu.:1.000 1st Qu.: 3.500 1st Qu.: 5.250
## Median :2.000 Median :2.000 Median : 6.000 Median : 7.000
## Mean :1.945 Mean :1.565 Mean : 5.724 Mean : 6.475
## 3rd Qu.:2.000 3rd Qu.:2.000 3rd Qu.: 7.500 3rd Qu.: 8.000
## Max. :2.000 Max. :2.000 Max. :10.000 Max. :10.000
## NAs :2 NAs :148 NAs :149
## impact3 impact4 impact5 impact6
## Min. : 1.000 Min. : 1.000 Min. : 1.000 Min. : 1.000
## 1st Qu.: 4.000 1st Qu.: 3.000 1st Qu.: 3.500 1st Qu.: 4.000
## Median : 6.000 Median : 5.000 Median : 6.000 Median : 6.000
## Mean : 5.713 Mean : 4.967 Mean : 5.341 Mean : 5.664
## 3rd Qu.: 7.750 3rd Qu.: 7.000 3rd Qu.: 7.000 3rd Qu.: 7.000
## Max. :10.000 Max. :10.000 Max. :10.000 Max. :10.000
## NAs :149 NAs :149 NAs :148 NAs :149
## impact7 stopb restlss drvsleep
## Min. : 1.000 Min. :1.000 Min. :1.000 Min. :1.000
## 1st Qu.: 2.000 1st Qu.:2.000 1st Qu.:1.000 1st Qu.:2.000
## Median : 5.000 Median :2.000 Median :2.000 Median :2.000
## Mean : 4.821 Mean :1.916 Mean :1.667 Mean :1.876
## 3rd Qu.: 7.000 3rd Qu.:2.000 3rd Qu.:2.000 3rd Qu.:2.000
## Max. :10.000 Max. :2.000 Max. :2.000 Max. :2.000
## NAs :148 NAs :8 NAs :7 NAs :22
## drvresul ess anxiety depress
## Min. :1.000 Min. : 0.000 Min. : 0.000 Min. : 0.000
## 1st Qu.:2.000 1st Qu.: 4.000 1st Qu.: 4.000 1st Qu.: 1.000
## Median :2.000 Median : 6.000 Median : 6.000 Median : 3.000
## Mean :1.908 Mean : 6.584 Mean : 6.336 Mean : 3.498
## 3rd Qu.:2.000 3rd Qu.: 9.000 3rd Qu.: 8.000 3rd Qu.: 5.000
## Max. :2.000 Max. :18.000 Max. :17.000 Max. :12.000
## NAs :21 NAs :16 NAs :3 NAs :2
## fatigue lethargy tired sleepy
## Min. : 1.00 Min. : 1.000 Min. : 1.000 Min. : 1.000
## 1st Qu.: 3.00 1st Qu.: 3.000 1st Qu.: 3.000 1st Qu.: 3.000
## Median : 5.00 Median : 4.000 Median : 6.000 Median : 6.000
## Mean : 4.88 Mean : 4.717 Mean : 5.296 Mean : 5.566
## 3rd Qu.: 7.00 3rd Qu.: 7.000 3rd Qu.: 7.000 3rd Qu.: 7.000
## Max. :10.00 Max. :10.000 Max. :10.000 Max. :10.000
## NAs :12 NAs :2 NAs :1 NAs :6
## energy stayslprec getsleprec qualsleeprec
## Min. : 1.00 Min. :0.0000 Min. :0.0000 Min. :1.000
## 1st Qu.: 3.00 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:2.000
## Median : 6.00 Median :0.0000 Median :0.0000 Median :3.000
## Mean : 5.53 Mean :0.4361 Mean :0.3941 Mean :2.701
## 3rd Qu.: 8.00 3rd Qu.:1.0000 3rd Qu.:1.0000 3rd Qu.:3.250
## Max. :10.00 Max. :1.0000 Max. :1.0000 Max. :4.000
## NAs :3 NAs :5 NAs :2 NAs :3
## totsas cigsgp3 agegp3 probsleeprec
## Min. : 5.00 Min. :1.000 Min. :1.000 Min. :0.0000
## 1st Qu.:17.00 1st Qu.:1.000 1st Qu.:1.000 1st Qu.:0.0000
## Median :27.00 Median :2.000 Median :2.000 Median :0.0000
## Mean :26.04 Mean :1.914 Mean :1.984 Mean :0.4349
## 3rd Qu.:35.00 3rd Qu.:3.000 3rd Qu.:3.000 3rd Qu.:1.0000
## Max. :50.00 Max. :3.000 Max. :3.000 Max. :1.0000
## NAs :20 NAs :236 NAs :23 NAs :2
## drvslprec
## Min. :0.0000
## 1st Qu.:0.0000
## Median :0.0000
## Mean :0.1245
## 3rd Qu.:0.0000
## Max. :1.0000
## NAs :22
#summary(data_sav$healthrate)
#summary(data_sav$smoke)
#summary(data_sav$fitrate)
#Displaying the structure of a dataset
str(data_sav)
## tibble [271 × 55] (S3: tbl_df/tbl/data.frame)
## $ id : num [1:271] 83 294 425 64 536 57 251 255 265 290 ...
## ..- attr(*, "label")= chr "Identification Number"
## ..- attr(*, "format.spss")= chr "F8.0"
## $ sex : dbl+lbl [1:271] 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, ...
## ..@ label : chr "sex"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 0 1
## .. ..- attr(*, "names")= chr [1:2] "female" "male"
## $ age : num [1:271] 42 54 NA 41 39 66 36 35 NA 41 ...
## ..- attr(*, "label")= chr "age"
## ..- attr(*, "format.spss")= chr "F8.0"
## $ marital : dbl+lbl [1:271] 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 4, 2, 2, 2, ...
## ..@ label : chr "marital status"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:4] 1 2 3 4
## .. ..- attr(*, "names")= chr [1:4] "single" "married/defacto" "divorced" "widowed"
## $ edlevel : dbl+lbl [1:271] 2, 5, 2, 5, 5, 4, 3, 5, 5, 5, 5, 4, 5, 5...
## ..@ label : chr "highest education level achieved"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:5] 1 2 3 4 5
## .. ..- attr(*, "names")= chr [1:5] "primary school" "secondary school" "trade training/ post secondary training" "undergraduate degree" ...
## $ weight : num [1:271] 52 65 89 66 62 62 62 75 90 75 ...
## ..- attr(*, "format.spss")= chr "F8.0"
## $ height : num [1:271] 162 174 170 178 160 165 165 174 180 187 ...
## ..- attr(*, "format.spss")= chr "F8.0"
## $ healthrate : dbl+lbl [1:271] 10, 8, 6, 9, 9, 8, 9, 6, 6, 9, 10, 8, 9, 8...
## ..@ label : chr "general health"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 10
## .. ..- attr(*, "names")= chr [1:2] "very poor" "very good"
## $ fitrate : dbl+lbl [1:271] 7, 7, 5, 7, 5, 8, 7, 6, 6, 9, 7, 8, 4, 6, 8, 6, 7, 8, ...
## ..@ label : chr "physical fitness"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 10
## .. ..- attr(*, "names")= chr [1:2] "very poor" "very good"
## $ weightrate : dbl+lbl [1:271] 5, 5, 7, 5, 7, 5, 7, 8, 8, 3, 6, 6, 7, 8, 5, 5, 8, 5, ...
## ..@ label : chr "current weight"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 10
## .. ..- attr(*, "names")= chr [1:2] "very underweight" "very overweight"
## $ smoke : dbl+lbl [1:271] 1, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, ...
## ..@ label : chr "do you smoke"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "yes" "no"
## $ smokenum : num [1:271] 15 NA NA 5 NA NA NA NA NA 7 ...
## ..- attr(*, "label")= chr "cigs per day"
## ..- attr(*, "format.spss")= chr "F8.0"
## $ alchohol : num [1:271] 3 0 12 2 1 1 0 2 0.25 1 ...
## ..- attr(*, "label")= chr "how many alcoholic drinks per day"
## ..- attr(*, "format.spss")= chr "F8.2"
## $ caffeine : num [1:271] 5 10 4 3 6 3 3 3 2 3 ...
## ..- attr(*, "label")= chr "how many caffeine drinks per day"
## ..- attr(*, "format.spss")= chr "F8.2"
## $ hourwnit : num [1:271] 9 6.5 6 7 7 7 6 6 7 6 ...
## ..- attr(*, "label")= chr "hours sleep/ week nights"
## ..- attr(*, "format.spss")= chr "F8.1"
## $ hourwend : num [1:271] 9 6.5 6 8 7 8 8 7 7 8 ...
## ..- attr(*, "label")= chr "hours sleep/ week ends"
## ..- attr(*, "format.spss")= chr "F8.1"
## $ hourneed : num [1:271] 9 7 8 8 7.5 8 8 7 8 7 ...
## ..- attr(*, "label")= chr "how many hours sleep needed"
## ..- attr(*, "format.spss")= chr "F8.1"
## $ trubslep : dbl+lbl [1:271] NA, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
## ..@ label : chr "trouble falling asleep?"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "yes" "no"
## $ trubstay : dbl+lbl [1:271] NA, 1, 1, 2, 1, 1, 1, 1, 2, 2, 1, 2, 2, 1...
## ..@ label : chr "trouble staying asleep"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "yes" "no"
## $ wakenite : dbl+lbl [1:271] 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, ...
## ..@ label : chr "waking during night"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "yes" "no"
## $ niteshft : dbl+lbl [1:271] 2, 2, 2, 2, 2, 2, 2, 2, NA, 2, 2, 1, 2, 1...
## ..@ label : chr "work night shift or rotating shifts"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:3] 1 2 3
## .. ..- attr(*, "names")= chr [1:3] "yes" "no" "sometimes"
## $ liteslp : dbl+lbl [1:271] 2, 1, 1, 2, 2, 2, 2, 1, NA, 2, 1, 1, 1, 1...
## ..@ label : chr "light sleeper?"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "yes" "no"
## $ refreshd : dbl+lbl [1:271] 1, 1, 2, 2, 1, 2, 2, 2, NA, 2, 2, 1, 1, 2...
## ..@ label : chr "do you feel refreshed weekdays?"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "yes" "no"
## $ satsleep : dbl+lbl [1:271] 2, 5, 3, 4, 2, 4, 6, 7, NA, 8, 3, 8, 8, 3...
## ..@ label : chr "satisfied with sleep amount"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 10
## .. ..- attr(*, "names")= chr [1:2] "very dissatisfied" "very satisfied"
## $ qualslp : dbl+lbl [1:271] 6, 4, 2, 4, 4, 3, 4, 5, NA, 5, 2, 4, 4, 2...
## ..@ label : chr "quality of sleep"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:6] 1 2 3 4 5 6
## .. ..- attr(*, "names")= chr [1:6] "very poor" "poor" "fair" "good" ...
## $ stressmo : dbl+lbl [1:271] 2, 5, 6, 8, 6, 7, 8, 8, 6, 5, 2, 5, 7, 8...
## ..@ label : chr "how stressed over last month?"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 10
## .. ..- attr(*, "names")= chr [1:2] "not at all" "extremely stressed"
## $ medhelp : dbl+lbl [1:271] 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, ...
## ..@ label : chr "medication taken to help sleep?"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "yes" "no"
## $ problem : dbl+lbl [1:271] 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 2, 1, 2, 2, 2, 2, ...
## ..@ label : chr "problem with sleep?"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "yes" "no"
## $ impact1 : dbl+lbl [1:271] NA, NA, NA, NA, NA, NA, NA, NA, 6, NA, 5, NA, NA, 9...
## ..@ label : chr "mood"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 10
## .. ..- attr(*, "names")= chr [1:2] "not at all" "to a great extent"
## $ impact2 : dbl+lbl [1:271] NA, NA, NA, NA, NA, NA, NA, NA, 6, NA, 7, NA, NA, 9...
## ..@ label : chr "energy level"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 10
## .. ..- attr(*, "names")= chr [1:2] "not at all" "to a great extent"
## $ impact3 : dbl+lbl [1:271] NA, NA, NA, NA, NA, NA, NA, NA, 6, NA, 8, NA, NA, 7...
## ..@ label : chr "concent"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 10
## .. ..- attr(*, "names")= chr [1:2] "not at all" "to a great extent"
## $ impact4 : dbl+lbl [1:271] NA, NA, NA, NA, NA, NA, NA, NA, 6, NA, 8, NA, NA, 7...
## ..@ label : chr "memory"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 10
## .. ..- attr(*, "names")= chr [1:2] "not at all" "to a great extent"
## $ impact5 : dbl+lbl [1:271] NA, NA, NA, NA, NA, NA, NA, NA, 7, NA, 7, NA, NA, 7...
## ..@ label : chr "life sat"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 10
## .. ..- attr(*, "names")= chr [1:2] "not at all" "to a great extent"
## $ impact6 : dbl+lbl [1:271] NA, NA, NA, NA, NA, NA, NA, NA, 8, NA, 6, NA, NA, 7...
## ..@ label : chr "overall well-being"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 10
## .. ..- attr(*, "names")= chr [1:2] "not at all" "to a great extent"
## $ impact7 : dbl+lbl [1:271] NA, NA, NA, NA, NA, NA, NA, NA, 8, NA, 3, NA, NA, 7...
## ..@ label : chr "relationships"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 10
## .. ..- attr(*, "names")= chr [1:2] "not at all" "to a great extent"
## $ stopb : dbl+lbl [1:271] 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2, ...
## ..@ label : chr "stop breathing"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "yes" "no"
## $ restlss : dbl+lbl [1:271] 2, 1, 1, 2, 2, 1, 2, 2, 1, 2, 1, 2, 1, 1, 2, 2, 2, 1, ...
## ..@ label : chr "restless sleeper"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "yes" "no"
## $ drvsleep : dbl+lbl [1:271] 2, 2, 1, 2, 2, NA, 2, 1, 2, 2, 2, 2, 2, 2...
## ..@ label : chr "asleep while driving"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "yes" "no"
## $ drvresul : dbl+lbl [1:271] 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, ...
## ..@ label : chr "drive, result in acc due to sleepy"
## ..@ format.spss: chr "F8.0"
## ..@ labels : Named num [1:2] 1 2
## .. ..- attr(*, "names")= chr [1:2] "yes" "no"
## $ ess : num [1:271] 8 17 13 12 12 10 10 10 10 10 ...
## ..- attr(*, "label")= chr "epworth sleepiness scale"
## ..- attr(*, "format.spss")= chr "F8.0"
## $ anxiety : num [1:271] 2 6 9 8 4 12 12 13 12 1 ...
## ..- attr(*, "label")= chr "HADS Anxiety"
## ..- attr(*, "format.spss")= chr "F8.0"
## $ depress : num [1:271] 1 2 10 3 0 6 10 11 11 0 ...
## ..- attr(*, "label")= chr "HADS Depression"
## ..- attr(*, "format.spss")= chr "F8.0"
## $ fatigue : num [1:271] 2 2 7 7 5 7 NA 7 7 2 ...
## ..- attr(*, "label")= chr "fatigued"
## ..- attr(*, "format.spss")= chr "F8.0"
## $ lethargy : num [1:271] 2 3 7 7 3 7 8 8 6 2 ...
## ..- attr(*, "label")= chr "lethargic"
## ..- attr(*, "format.spss")= chr "F8.0"
## $ tired : num [1:271] 2 5 6 6 5 6 8 8 7 4 ...
## ..- attr(*, "label")= chr "tired"
## ..- attr(*, "format.spss")= chr "F8.0"
## $ sleepy : num [1:271] 2 5 6 6 6 6 8 6 NA 5 ...
## ..- attr(*, "label")= chr "sleepy"
## ..- attr(*, "format.spss")= chr "F8.0"
## $ energy : num [1:271] 2 5 5 8 6 7 9 8 NA 3 ...
## ..- attr(*, "label")= chr "lack energy"
## ..- attr(*, "format.spss")= chr "F8.0"
## $ stayslprec : dbl+lbl [1:271] NA, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1...
## ..@ label : chr "prob stay asleep rec"
## ..@ format.spss : chr "F8.0"
## ..@ display_width: int 10
## ..@ labels : Named num [1:2] 0 1
## .. ..- attr(*, "names")= chr [1:2] "no" "yes"
## $ getsleprec : dbl+lbl [1:271] NA, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
## ..@ label : chr "prob fall asleep rec"
## ..@ format.spss : chr "F8.0"
## ..@ display_width: int 11
## ..@ labels : Named num [1:2] 0 1
## .. ..- attr(*, "names")= chr [1:2] "no" "yes"
## $ qualsleeprec: dbl+lbl [1:271] 4, 3, 1, 3, 3, 2, 3, 4, NA, 4, 1, 3, 3, 1...
## ..@ label : chr "qual sleep rec"
## ..@ format.spss : chr "F8.0"
## ..@ display_width: int 12
## ..@ labels : Named num [1:4] 1 2 3 4
## .. ..- attr(*, "names")= chr [1:4] "very poor, poor" "fair" "good" "very good, excellent"
## $ totsas : num [1:271] 10 20 31 34 25 33 NA 37 NA 16 ...
## ..- attr(*, "label")= chr "sleepy & assoc sensations scale"
## ..- attr(*, "format.spss")= chr "F8.0"
## $ cigsgp3 : dbl+lbl [1:271] 2, NA, NA, 1, NA, NA, NA, NA, NA, 2, NA, NA, NA, NA...
## ..@ label : chr "cigs per day gp3"
## ..@ format.spss: chr "F5.0"
## ..@ labels : Named num [1:3] 1 2 3
## .. ..- attr(*, "names")= chr [1:3] "<= 5" "6 - 15" "16+"
## $ agegp3 : dbl+lbl [1:271] 2, 3, NA, 2, 2, 3, 1, 1, NA, 2, NA, NA, 1, NA...
## ..@ label : chr "agegp3"
## ..@ format.spss: chr "F5.0"
## ..@ labels : Named num [1:3] 1 2 3
## .. ..- attr(*, "names")= chr [1:3] "<= 37" "38 - 50" "51+"
## $ probsleeprec: dbl+lbl [1:271] 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, ...
## ..@ label : chr "prob sleep recode 01"
## ..@ format.spss : chr "F8.0"
## ..@ display_width: int 10
## ..@ labels : Named num [1:2] 0 1
## .. ..- attr(*, "names")= chr [1:2] "no" "yes"
## $ drvslprec : dbl+lbl [1:271] 0, 0, 1, 0, 0, NA, 0, 1, 0, 0, 0, 0, 0, 0...
## ..@ label : chr "fall asleep while driving"
## ..@ format.spss : chr "F8.0"
## ..@ display_width: int 9
## ..@ labels : Named num [1:2] 0 1
## .. ..- attr(*, "names")= chr [1:2] "no" "yes"
# Getting the first 6 rows of the dataset
head(data_sav)
## # A tibble: 6 × 55
## id sex age marital edlevel weight height healthrate fitrate
## <dbl> <dbl+lbl> <dbl> <dbl+lbl> <dbl+l> <dbl> <dbl> <dbl+lbl> <dbl+l>
## 1 83 0 [female] 42 2 [married/de… 2 [sec… 52 162 10 [very … 7
## 2 294 0 [female] 54 2 [married/de… 5 [pos… 65 174 8 7
## 3 425 1 [male] NA 2 [married/de… 2 [sec… 89 170 6 5
## 4 64 0 [female] 41 2 [married/de… 5 [pos… 66 178 9 7
## 5 536 0 [female] 39 2 [married/de… 5 [pos… 62 160 9 5
## 6 57 0 [female] 66 2 [married/de… 4 [und… 62 165 8 8
## # ℹ 46 more variables: weightrate <dbl+lbl>, smoke <dbl+lbl>, smokenum <dbl>,
## # alchohol <dbl>, caffeine <dbl>, hourwnit <dbl>, hourwend <dbl>,
## # hourneed <dbl>, trubslep <dbl+lbl>, trubstay <dbl+lbl>, wakenite <dbl+lbl>,
## # niteshft <dbl+lbl>, liteslp <dbl+lbl>, refreshd <dbl+lbl>,
## # satsleep <dbl+lbl>, qualslp <dbl+lbl>, stressmo <dbl+lbl>,
## # medhelp <dbl+lbl>, problem <dbl+lbl>, impact1 <dbl+lbl>, impact2 <dbl+lbl>,
## # impact3 <dbl+lbl>, impact4 <dbl+lbl>, impact5 <dbl+lbl>, …
# Getting the last 6 rows of the dataset
tail(data_sav)
## # A tibble: 6 × 55
## id sex age marital edlevel weight height healthrate fitrate
## <dbl> <dbl+lbl> <dbl> <dbl+lbl> <dbl+l> <dbl> <dbl> <dbl+lbl> <dbl+lb>
## 1 256 1 [male] 58 2 [married/d… 5 [pos… 82 155 10 [very … 6
## 2 286 1 [male] 56 2 [married/d… 5 [pos… 90 175 4 3
## 3 271 0 [female] 55 3 [divorced] 4 [und… 70 162. 5 3
## 4 288 0 [female] 55 2 [married/d… 2 [sec… NA NA 10 [very … 10 [ver…
## 5 423 0 [female] 71 2 [married/d… 3 [tra… 68 168 9 9
## 6 427 1 [male] 62 2 [married/d… 3 [tra… 113 189 5 5
## # ℹ 46 more variables: weightrate <dbl+lbl>, smoke <dbl+lbl>, smokenum <dbl>,
## # alchohol <dbl>, caffeine <dbl>, hourwnit <dbl>, hourwend <dbl>,
## # hourneed <dbl>, trubslep <dbl+lbl>, trubstay <dbl+lbl>, wakenite <dbl+lbl>,
## # niteshft <dbl+lbl>, liteslp <dbl+lbl>, refreshd <dbl+lbl>,
## # satsleep <dbl+lbl>, qualslp <dbl+lbl>, stressmo <dbl+lbl>,
## # medhelp <dbl+lbl>, problem <dbl+lbl>, impact1 <dbl+lbl>, impact2 <dbl+lbl>,
## # impact3 <dbl+lbl>, impact4 <dbl+lbl>, impact5 <dbl+lbl>, …
#importing Data from Data from text files
#This dataset (reed.xml) is an XML dataset that contains information about university courses and classes.
library(xml2)
data_xml <- read_xml("D:/Personal/Masters/R_programming/Fina_assignment/reed.xml")
courses <- xml_text(xml_find_all(data_xml, ".//title"))
print("Getting first 6 courses\n")
## [1] "Getting first 6 courses\n"
head(courses, 6)
## [1] "Introduction to Anthropology" "Sex and Gender"
## [3] "Field Biology of Amphibians" "Bacterial Pathogenesis"
## [5] "Seminar in Biology" "MolecularStructure and Properties"
print("Getting last 6 courses\n")
## [1] "Getting last 6 courses\n"
tail(courses, 6)
## [1] "Animal Physiology" "Animal Physiology" "Animal Physiology"
## [4] "Evolutionary Biology" "Evolutionary Biology" "Evolutionary Biology"
print("Summary of courses\n")
## [1] "Summary of courses\n"
summary(courses)
## Length N.unique N.blank Min.nchar Max.nchar
## 703 394 0 4 37
#importing from database PostgreSQL
library(DBI)
library(RPostgres)
con <- dbConnect(
RPostgres::Postgres(),
dbname = "R_learning",
host = "localhost",
port = 5432,
user = "postgres",
password = "qwert1234"
)
dbListTables(con)
## [1] "Student"
data_database <- dbGetQuery(con, 'SELECT * FROM "Student"')
head(data_database)
## id name phone
## 1 1 Divin 789794785
## 2 2 Claudel 788906438
str(data_database)
## 'data.frame': 2 obs. of 3 variables:
## $ id : int 1 2
## $ name : chr "Divin" "Claudel"
## $ phone: int 789794785 788906438
The assignment is about merging two dataset using two or three variables, in my codes I used three variables id, name and year
# Install package (run once if not installed)
#install.packages("dplyr")
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
# Import CSV files
salary_data <- read.csv("D:/Personal/Masters/R_programming/Fina_assignment/dataset_salary.csv")
department_data <- read.csv("D:/Personal/Masters/R_programming/Fina_assignment/dataset_department.csv")
# View first rows
head(salary_data)
## id name year salary
## 1 1 Cindy Martinez 2026 3608
## 2 2 Mr. Robert Estrada 2024 2171
## 3 3 Amy Savage 2026 4180
## 4 4 Jessica Davis 2023 3172
## 5 5 Yesenia Garcia 2023 934
## 6 6 Richard Young 2023 4556
head(department_data)
## id name year department
## 1 1 Cindy Martinez 2026 IT
## 2 2 Mr. Robert Estrada 2024 Operations
## 3 3 Amy Savage 2026 Marketing
## 4 4 Jessica Davis 2023 Finance
## 5 5 Yesenia Garcia 2023 Finance
## 6 6 Richard Young 2023 Operations
# Merge datasets using 3 variables:
# id, name, and year
merged_data <- merge(
salary_data,
department_data,
by = c("id", "name", "year")
)
# Display merged dataset
head(merged_data)
## id name year salary department
## 1 1 Cindy Martinez 2026 3608 IT
## 2 10 Brian Watson 2026 3468 Marketing
## 3 100 Timothy Knight 2022 2975 HR
## 4 101 Aaron Griffin 2025 796 Finance
## 5 102 Tammy Reilly 2022 2957 Marketing
## 6 103 Louis Cortez 2024 1321 Sales
# Check structure
str(merged_data)
## 'data.frame': 200 obs. of 5 variables:
## $ id : int 1 10 100 101 102 103 104 105 106 107 ...
## $ name : chr "Cindy Martinez" "Brian Watson" "Timothy Knight" "Aaron Griffin" ...
## $ year : int 2026 2026 2022 2025 2022 2024 2026 2022 2024 2022 ...
## $ salary : int 3608 3468 2975 796 2957 1321 3160 1843 1215 851 ...
## $ department: chr "IT" "Marketing" "HR" "Finance" ...
# Check number of rows and columns
dim(merged_data)
## [1] 200 5
# Save merged dataset into CSV
write.csv(
merged_data,
"D:/Personal/Masters/R_programming/Fina_assignment/merged_dataset.csv",
row.names = FALSE
)
# View full dataset
View(merged_data)
Using group by $ %>%
# Install package (run once if not installed)
#install.packages("dplyr")
library(dplyr)
# Import CSV files
salary_data <- read.csv("D:/Personal/Masters/R_programming/Fina_assignment/dataset_salary.csv")
department_data <- read.csv("D:/Personal/Masters/R_programming/Fina_assignment/dataset_department.csv")
# View first rows
#head(salary_data)
#head(department_data)
# Merge datasets using 3 variables:
# id, name, and year
merged_data <- merge(
salary_data,
department_data,
by = c("id", "name", "year")
)
# Display merged dataset
#head(merged_data)
# Check structure
#str(merged_data)
# Check number of rows and columns
#dim(merged_data)
# Save merged dataset into CSV
write.csv(
merged_data,
"D:/Personal/Masters/R_programming/Fina_assignment/merged_dataset.csv",
row.names = FALSE
)
# View full dataset
#View(merged_data)
merged_data %>%
group_by(department) %>%
summarise(
average = mean(salary),
total = sum(salary),
count = n()
)
## # A tibble: 7 × 4
## department average total count
## <chr> <dbl> <int> <int>
## 1 Finance 2961. 91780 31
## 2 HR 2885. 95206 33
## 3 IT 2836. 59565 21
## 4 Marketing 2510. 62746 25
## 5 Operations 2743. 68570 25
## 6 Sales 2843. 96667 34
## 7 Support 2564. 79493 31
merged_data %>%
group_by(department) %>%
summarise(total_salary = sum(salary))
## # A tibble: 7 × 2
## department total_salary
## <chr> <int>
## 1 Finance 91780
## 2 HR 95206
## 3 IT 59565
## 4 Marketing 62746
## 5 Operations 68570
## 6 Sales 96667
## 7 Support 79493
merged_data %>%
group_by(department) %>%
summarise(total_employee = n())
## # A tibble: 7 × 2
## department total_employee
## <chr> <int>
## 1 Finance 31
## 2 HR 33
## 3 IT 21
## 4 Marketing 25
## 5 Operations 25
## 6 Sales 34
## 7 Support 31
merged_data %>%
group_by(department) %>%
summarise(avg_salary = mean(salary))
## # A tibble: 7 × 2
## department avg_salary
## <chr> <dbl>
## 1 Finance 2961.
## 2 HR 2885.
## 3 IT 2836.
## 4 Marketing 2510.
## 5 Operations 2743.
## 6 Sales 2843.
## 7 Support 2564.
How to use trace() and recover()
library(dplyr)
# Import CSV files
salary_data <- read.csv(
"D:/Personal/Masters/R_programming/Fina_assignment/dataset_salary.csv"
)
department_data <- read.csv(
"D:/Personal/Masters/R_programming/Fina_assignment/dataset_department.csv"
)
# Function to merge datasets
merge_data <- function() {
merge(
salary_data,
department_data,
by = c("id", "name", "year")
)
}
# Function to calculate grouped statistics
group_salary <- function() {
data <- merge_data()
# Intentional error:
# salaryy does not exist
data %>%
group_by(department) %>%
summarise(avg_salary = mean(salaryy))
}
# Run function
group_salary()
## Error in `summarise()`:
## ℹ In argument: `avg_salary = mean(salaryy)`.
## ℹ In group 1: `department = "Finance"`.
## Caused by error:
## ! object 'salaryy' not found
# Check traceback after error
traceback()
## No traceback available
library(dplyr)
# Turn on recover mode
options(error = recover)
# Import datasets
salary_data <- read.csv(
"D:/Personal/Masters/R_programming/Fina_assignment/dataset_salary.csv"
)
department_data <- read.csv(
"D:/Personal/Masters/R_programming/Fina_assignment/dataset_department.csv"
)
# Function to merge datasets
merge_data <- function() {
merge(
salary_data,
department_data,
by = c("id", "name", "year")
)
}
# Function with intentional error
group_salary <- function() {
data <- merge_data()
# Error:
# salaryy column does not exist
data %>%
group_by(department) %>%
summarise(
avg_salary = mean(salaryy)
)
}
# Run function
group_salary()
## Error in `summarise()`:
## ℹ In argument: `avg_salary = mean(salaryy)`.
## ℹ In group 1: `department = "Finance"`.
## Caused by error:
## ! object 'salaryy' not found
Make functions that calculate summary statistics and apply it to a variable to show that it works
# Load dataset
merge_data <- read.csv("merged_dataset.csv")
# Function for summary statistics
statistics_function <- function(x) {
print("Mean")
print("========")
print(mean(x))
print("Median")
print("========")
print(median(x))
print("Minimum")
print("========")
print(min(x))
print("Maximum")
print("========")
print(max(x))
print("Standard Deviation")
print("===================")
print(sd(x))
print("Variance")
print("========")
print(var(x))
print("Total")
print("========")
print(sum(x))
print("Count")
print("========")
print(length(x))
}
statistics_function(merge_data$salary)
## [1] "Mean"
## [1] "========"
## [1] 2770.135
## [1] "Median"
## [1] "========"
## [1] 2782.5
## [1] "Minimum"
## [1] "========"
## [1] 418
## [1] "Maximum"
## [1] "========"
## [1] 4992
## [1] "Standard Deviation"
## [1] "==================="
## [1] 1331.708
## [1] "Variance"
## [1] "========"
## [1] 1773446
## [1] "Total"
## [1] "========"
## [1] 554027
## [1] "Count"
## [1] "========"
## [1] 200
# Create function for two sample t-test
#Make a function to calculate two sample t test, then apply it to a function
a <- c(10, 12, 14, 15, 16)
b <- c(20, 22, 24, 25, 26)
# Function for two sample t-test
t_test_function <- function(x, y) {
result <- t.test(x, y)
print(result)
}
t_test_function(a, b)
##
## Welch Two Sample t-test
##
## data: x and y
## t = -6.5653, df = 8, p-value = 0.0001756
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -13.512401 -6.487599
## sample estimates:
## mean of x mean of y
## 13.4 23.4
# Load merged dataset
merged_dataset <- read.csv("merged_dataset.csv")
# lapply()
# Returns results as a list
lapply(merged_dataset[, c("salary", "year")], mean)
## $salary
## [1] 2770.135
##
## $year
## [1] 2024.025
# sapply()
# Returns results as vectors or matrices
sapply(merged_dataset[, c("salary", "year")], mean)
## salary year
## 2770.135 2024.025
# vapply()
# Similar to sapply() but requires output type
vapply(
merged_dataset[, c("salary", "year")],
mean,
numeric(1)
)
## salary year
## 2770.135 2024.025
# mapply()
# Applies function to multiple variables at the same time
mapply(
sum,
merged_dataset$salary,
merged_dataset$year
)
## [1] 5634 5494 4997 2821 4979 3345 5186 3865 3239 2873 6816 3385 6693 4518 3573
## [16] 6283 2443 5546 3139 4578 5889 5560 6447 6574 6671 3568 3437 4620 2829 2882
## [31] 6302 2746 3435 6939 3675 2941 3434 5115 4221 4629 6325 3951 6419 4630 3634
## [46] 3119 4099 4972 2494 2735 6344 4464 4708 4641 5111 3030 3586 4976 5075 2841
## [61] 5266 2551 3154 6209 6529 4626 4236 6738 6197 5323 6573 3006 5922 6946 4187
## [76] 3894 5823 4117 5926 5766 6289 3798 5740 4454 2579 6636 6159 3758 3788 5340
## [91] 2944 5323 6719 5820 4399 4560 5829 4575 5903 3089 5616 6816 6877 2643 4308
## [106] 2535 6394 4238 4769 3921 5800 4195 7017 4956 6491 5352 5468 6876 5285 5496
## [121] 5764 5752 6796 6206 3134 6970 5340 5386 3275 3521 5159 3187 6894 6873 5195
## [136] 3155 5240 5912 6629 4018 4132 4888 3318 4648 5170 2957 2750 4359 3840 3710
## [151] 4184 3101 5385 6283 3125 2845 6579 4884 5978 4918 6906 5190 4086 4323 6952
## [166] 5154 3722 2687 6898 3811 2847 5050 6563 4358 4680 4138 4389 4498 3819 5997
## [181] 4141 5574 3645 4703 2939 6268 5558 6639 6635 6282 4840 3724 2548 4851 2982
## [196] 3336 5627 3658 6448 6687