1. Loaded Data is Already Tibble

# load heart.csv directly from my website
library(tidyverse)

Heart <- read_csv("https://nmimoto.github.io/datasets/heart.csv")

# Description of the data is here:
# https://nmimoto.github.io/datasets/heart.txt

# print(Heart, n=100)        # use this if you want to see more rows
# print(Heart, width=1000)   # use this if you want to see more columns

Heart       # shows you a bit of data with some info.
## # A tibble: 303 × 15
##    index   Age   Sex ChestPain    RestBP  Chol   Fbs RestECG MaxHR ExAng Oldpeak
##    <dbl> <dbl> <dbl> <chr>         <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl>   <dbl>
##  1     1    63     1 typical         145   233     1       2   150     0     2.3
##  2     2    67     1 asymptomatic    160   286     0       2   108     1     1.5
##  3     3    67     1 asymptomatic    120   229     0       2   129     1     2.6
##  4     4    37     1 nonanginal      130   250     0       0   187     0     3.5
##  5     5    41     0 nontypical      130   204     0       2   172     0     1.4
##  6     6    56     1 nontypical      120   236     0       0   178     0     0.8
##  7     7    62     0 asymptomatic    140   268     0       2   160     0     3.6
##  8     8    57     0 asymptomatic    120   354     0       0   163     1     0.6
##  9     9    63     1 asymptomatic    130   254     0       2   147     0     1.4
## 10    10    53     1 asymptomatic    140   203     1       2   155     1     3.1
## # ℹ 293 more rows
## # ℹ 4 more variables: Slope <dbl>, Ca <dbl>, Thal <chr>, AHD <chr>
class(Heart)               # it's tibble
## [1] "spec_tbl_df" "tbl_df"      "tbl"         "data.frame"
is.data.frame(Heart)       # tibble is also a dataframe.
## [1] TRUE
names(Heart)               # all the column names of the tibble
##  [1] "index"     "Age"       "Sex"       "ChestPain" "RestBP"    "Chol"     
##  [7] "Fbs"       "RestECG"   "MaxHR"     "ExAng"     "Oldpeak"   "Slope"    
## [13] "Ca"        "Thal"      "AHD"


2. You can make DF a Tibble

iris                    # this is built-in data
class(iris)             # it’s data.frame
## [1] "data.frame"
is.data.frame(iris)
## [1] TRUE
is_tibble(iris)         # not a tibble
## [1] FALSE
library(tidyverse)
iris = as_tibble(iris)  # turn it into a tibble
is.tibble(iris)         # now it’s a tibble
## Warning: `is.tibble()` was deprecated in tibble 2.0.0.
## ℹ Please use `is_tibble()` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## [1] TRUE
is.data.frame(iris)     # it’s still a data.frame
## [1] TRUE
# Note the difference in how it is shown on screen
iris
## # A tibble: 150 × 5
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
##           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
##  1          5.1         3.5          1.4         0.2 setosa 
##  2          4.9         3            1.4         0.2 setosa 
##  3          4.7         3.2          1.3         0.2 setosa 
##  4          4.6         3.1          1.5         0.2 setosa 
##  5          5           3.6          1.4         0.2 setosa 
##  6          5.4         3.9          1.7         0.4 setosa 
##  7          4.6         3.4          1.4         0.3 setosa 
##  8          5           3.4          1.5         0.2 setosa 
##  9          4.4         2.9          1.4         0.2 setosa 
## 10          4.9         3.1          1.5         0.1 setosa 
## # ℹ 140 more rows


3. Select a column in Tibble

Heart            # this is a tibble

Heart["index"]   # select column with name “index”
Heart[1]         # select 1st column (note that there’s no comma)
Heart[ ,1]       # select 1st column (same as the case of matrix)
Heart[1, ]       # select 1st row (same as the case of matrix)
Heart$index      # another way

## You can also use pipe operator “%>%”
## and function select to do the same
Heart %>% select(index)     # select column with name index
Heart %>% select("index")   # quotes are optional


4. Pipe operator

## Make a table of ChestPain column in Heart data
table(Heart["ChestPain"])                # old school way
table(Heart$ChestPain)                   # also an old school way
# Use pipe operator and write
Heart %>% select(ChestPain) %>% table    # new school
## ChestPain
## asymptomatic   nonanginal   nontypical      typical 
##          144           86           50           23


5. Check Class

# Method 1
# Apply class() to each column of Heart.  Answer is a list, and hard to read.
Heart %>% lapply(class)
## $index
## [1] "numeric"
## 
## $Age
## [1] "numeric"
## 
## $Sex
## [1] "numeric"
## 
## $ChestPain
## [1] "character"
## 
## $RestBP
## [1] "numeric"
## 
## $Chol
## [1] "numeric"
## 
## $Fbs
## [1] "numeric"
## 
## $RestECG
## [1] "numeric"
## 
## $MaxHR
## [1] "numeric"
## 
## $ExAng
## [1] "numeric"
## 
## $Oldpeak
## [1] "numeric"
## 
## $Slope
## [1] "numeric"
## 
## $Ca
## [1] "numeric"
## 
## $Thal
## [1] "character"
## 
## $AHD
## [1] "character"
# make the answer not list
Heart %>% lapply(class) %>% unlist
##       index         Age         Sex   ChestPain      RestBP        Chol 
##   "numeric"   "numeric"   "numeric" "character"   "numeric"   "numeric" 
##         Fbs     RestECG       MaxHR       ExAng     Oldpeak       Slope 
##   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric" 
##          Ca        Thal         AHD 
##   "numeric" "character" "character"
# Method 2
# this is a good way to check class if you can see all the columns
Heart
## # A tibble: 303 × 15
##    index   Age   Sex ChestPain    RestBP  Chol   Fbs RestECG MaxHR ExAng Oldpeak
##    <dbl> <dbl> <dbl> <chr>         <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl>   <dbl>
##  1     1    63     1 typical         145   233     1       2   150     0     2.3
##  2     2    67     1 asymptomatic    160   286     0       2   108     1     1.5
##  3     3    67     1 asymptomatic    120   229     0       2   129     1     2.6
##  4     4    37     1 nonanginal      130   250     0       0   187     0     3.5
##  5     5    41     0 nontypical      130   204     0       2   172     0     1.4
##  6     6    56     1 nontypical      120   236     0       0   178     0     0.8
##  7     7    62     0 asymptomatic    140   268     0       2   160     0     3.6
##  8     8    57     0 asymptomatic    120   354     0       0   163     1     0.6
##  9     9    63     1 asymptomatic    130   254     0       2   147     0     1.4
## 10    10    53     1 asymptomatic    140   203     1       2   155     1     3.1
## # ℹ 293 more rows
## # ℹ 4 more variables: Slope <dbl>, Ca <dbl>, Thal <chr>, AHD <chr>
# If the columns are too many
Heart[1:7]
## # A tibble: 303 × 7
##    index   Age   Sex ChestPain    RestBP  Chol   Fbs
##    <dbl> <dbl> <dbl> <chr>         <dbl> <dbl> <dbl>
##  1     1    63     1 typical         145   233     1
##  2     2    67     1 asymptomatic    160   286     0
##  3     3    67     1 asymptomatic    120   229     0
##  4     4    37     1 nonanginal      130   250     0
##  5     5    41     0 nontypical      130   204     0
##  6     6    56     1 nontypical      120   236     0
##  7     7    62     0 asymptomatic    140   268     0
##  8     8    57     0 asymptomatic    120   354     0
##  9     9    63     1 asymptomatic    130   254     0
## 10    10    53     1 asymptomatic    140   203     1
## # ℹ 293 more rows
Heart[8:15]
## # A tibble: 303 × 8
##    RestECG MaxHR ExAng Oldpeak Slope    Ca Thal       AHD  
##      <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl> <chr>      <chr>
##  1       2   150     0     2.3     3     0 fixed      No   
##  2       2   108     1     1.5     2     3 normal     Yes  
##  3       2   129     1     2.6     2     2 reversable Yes  
##  4       0   187     0     3.5     3     0 normal     No   
##  5       2   172     0     1.4     1     0 normal     No   
##  6       0   178     0     0.8     1     0 normal     No   
##  7       2   160     0     3.6     3     2 normal     Yes  
##  8       0   163     1     0.6     1     0 normal     No   
##  9       2   147     0     1.4     2     1 reversable Yes  
## 10       2   155     1     3.1     3     0 reversable Yes  
## # ℹ 293 more rows
# Method 3
# This will show all the columns and their class
glimpse(Heart)
## Rows: 303
## Columns: 15
## $ index     <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1…
## $ Age       <dbl> 63, 67, 67, 37, 41, 56, 62, 57, 63, 53, 57, 56, 56, 44, 52, …
## $ Sex       <dbl> 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, …
## $ ChestPain <chr> "typical", "asymptomatic", "asymptomatic", "nonanginal", "no…
## $ RestBP    <dbl> 145, 160, 120, 130, 130, 120, 140, 120, 130, 140, 140, 140, …
## $ Chol      <dbl> 233, 286, 229, 250, 204, 236, 268, 354, 254, 203, 192, 294, …
## $ Fbs       <dbl> 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, …
## $ RestECG   <dbl> 2, 2, 2, 0, 2, 0, 2, 0, 2, 2, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, …
## $ MaxHR     <dbl> 150, 108, 129, 187, 172, 178, 160, 163, 147, 155, 148, 153, …
## $ ExAng     <dbl> 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, …
## $ Oldpeak   <dbl> 2.3, 1.5, 2.6, 3.5, 1.4, 0.8, 3.6, 0.6, 1.4, 3.1, 0.4, 1.3, …
## $ Slope     <dbl> 3, 2, 2, 3, 1, 1, 3, 1, 2, 3, 2, 2, 2, 1, 1, 1, 3, 1, 1, 1, …
## $ Ca        <dbl> 0, 3, 2, 0, 0, 0, 2, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, …
## $ Thal      <chr> "fixed", "normal", "reversable", "normal", "normal", "normal…
## $ AHD       <chr> "No", "Yes", "Yes", "No", "No", "No", "Yes", "No", "Yes", "Y…


6. Rename Column Names

# rename a column
Heart %>% rename(Cpain = ChestPain) %>%
              rename(ix_new = 1)         # rename 1st col
## # A tibble: 303 × 15
##    ix_new   Age   Sex Cpain RestBP  Chol   Fbs RestECG MaxHR ExAng Oldpeak Slope
##     <dbl> <dbl> <dbl> <chr>  <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl>   <dbl> <dbl>
##  1      1    63     1 typi…    145   233     1       2   150     0     2.3     3
##  2      2    67     1 asym…    160   286     0       2   108     1     1.5     2
##  3      3    67     1 asym…    120   229     0       2   129     1     2.6     2
##  4      4    37     1 nona…    130   250     0       0   187     0     3.5     3
##  5      5    41     0 nont…    130   204     0       2   172     0     1.4     1
##  6      6    56     1 nont…    120   236     0       0   178     0     0.8     1
##  7      7    62     0 asym…    140   268     0       2   160     0     3.6     3
##  8      8    57     0 asym…    120   354     0       0   163     1     0.6     1
##  9      9    63     1 asym…    130   254     0       2   147     0     1.4     2
## 10     10    53     1 asym…    140   203     1       2   155     1     3.1     3
## # ℹ 293 more rows
## # ℹ 3 more variables: Ca <dbl>, Thal <chr>, AHD <chr>


7. Change order of Columns

# Bring a column to 1st
Heart %>% relocate(AHD)
## # A tibble: 303 × 15
##    AHD   index   Age   Sex ChestPain    RestBP  Chol   Fbs RestECG MaxHR ExAng
##    <chr> <dbl> <dbl> <dbl> <chr>         <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl>
##  1 No        1    63     1 typical         145   233     1       2   150     0
##  2 Yes       2    67     1 asymptomatic    160   286     0       2   108     1
##  3 Yes       3    67     1 asymptomatic    120   229     0       2   129     1
##  4 No        4    37     1 nonanginal      130   250     0       0   187     0
##  5 No        5    41     0 nontypical      130   204     0       2   172     0
##  6 No        6    56     1 nontypical      120   236     0       0   178     0
##  7 Yes       7    62     0 asymptomatic    140   268     0       2   160     0
##  8 No        8    57     0 asymptomatic    120   354     0       0   163     1
##  9 Yes       9    63     1 asymptomatic    130   254     0       2   147     0
## 10 Yes      10    53     1 asymptomatic    140   203     1       2   155     1
## # ℹ 293 more rows
## # ℹ 4 more variables: Oldpeak <dbl>, Slope <dbl>, Ca <dbl>, Thal <chr>
# use .before or .after to specify
Heart %>% relocate(AHD,    .after=Age) %>%
        relocate(MaxHR, .before=Age)
## # A tibble: 303 × 15
##    index MaxHR   Age AHD     Sex ChestPain    RestBP  Chol   Fbs RestECG ExAng
##    <dbl> <dbl> <dbl> <chr> <dbl> <chr>         <dbl> <dbl> <dbl>   <dbl> <dbl>
##  1     1   150    63 No        1 typical         145   233     1       2     0
##  2     2   108    67 Yes       1 asymptomatic    160   286     0       2     1
##  3     3   129    67 Yes       1 asymptomatic    120   229     0       2     1
##  4     4   187    37 No        1 nonanginal      130   250     0       0     0
##  5     5   172    41 No        0 nontypical      130   204     0       2     0
##  6     6   178    56 No        1 nontypical      120   236     0       0     0
##  7     7   160    62 Yes       0 asymptomatic    140   268     0       2     0
##  8     8   163    57 No        0 asymptomatic    120   354     0       0     1
##  9     9   147    63 Yes       1 asymptomatic    130   254     0       2     0
## 10    10   155    53 Yes       1 asymptomatic    140   203     1       2     1
## # ℹ 293 more rows
## # ℹ 4 more variables: Oldpeak <dbl>, Slope <dbl>, Ca <dbl>, Thal <chr>


8. NA handling

library(tidyverse)

# load fake sample data
Exam1 = read_csv("https://nmimoto.github.io/datasets/examsSample.csv")
Exam1
## # A tibble: 45 × 13
##        ID First  Last  `Grade Basis` Units `Program and Plan` Level Status    E1
##     <dbl> <chr>  <chr> <chr>         <dbl> <chr>              <chr> <chr>  <dbl>
##  1 609204 Marsh… Kelly ABC/NC            4 Engineering under… Juni… Enrol…    35
##  2 369128 Wicke… Jere… ABC/NC            4 Arts & Sciences u… Seni… Enrol…    42
##  3 817608 O'Nei… Nath… ABC/NC            4 Engineering under… Juni… Enrol…    45
##  4 460606 Antal… Miles ABC/NC            4 Engineering under… Seni… Enrol…    38
##  5 723419 Valai… Kree  ABC/NC            4 Arts & Sciences u… Juni… Enrol…    34
##  6 593710 Child… Nich… ABC/NC            4 Arts & Sciences u… 5th … Enrol…    36
##  7 879944 Willi… David ABC/NC            4 Engineering under… 5th … Enrol…    37
##  8 204113 Wesel  Elyse ABC/NC            4 Engineering under… 5th … Enrol…    45
##  9 866737 Matus… Amy   ABC/NC            4 Engineering under… Juni… Enrol…    45
## 10 629622 Wheel… Alex… ABC/NC            4 The University of… Soph… Enrol…    42
## # ℹ 35 more rows
## # ℹ 4 more variables: E2 <dbl>, E3 <dbl>, E4 <dbl>, Final <dbl>
names(Exam1)
##  [1] "ID"               "First"            "Last"             "Grade Basis"     
##  [5] "Units"            "Program and Plan" "Level"            "Status"          
##  [9] "E1"               "E2"               "E3"               "E4"              
## [13] "Final"
# see how many NA is in the data
sum(is.na(Exam1))
## [1] 5
# number of NA for each column
Exam1 %>% lapply(function(x) sum(is.na(x))) %>% unlist
##               ID            First             Last      Grade Basis 
##                0                0                0                0 
##            Units Program and Plan            Level           Status 
##                0                0                0                0 
##               E1               E2               E3               E4 
##                0                1                1                1 
##            Final 
##                2
# number of NA for one column
Exam1 %>% select(Final) %>% is.na %>% sum
## [1] 2


9. View Dataframe as Spreadsheet

view(Heart)     # opens spreadsheet like viewer (not recommended)


10. Export Dataframe

write.csv(Heart, file="temp.csv")

library(writexl)  # install.packages("writexl")
write_xlsx(Heart, "temp.xlsx")

save(Heart, file="temp.Rdata")



CODE ONLY

##------------------------------
## 1. Loaded Data is Already Tibble

# load heart.csv directly from my website
library(tidyverse)

Heart <- read_csv("https://nmimoto.github.io/datasets/heart.csv")

# Description of the data is here:
# https://nmimoto.github.io/datasets/heart.txt

# print(Heart, n=100)        # use this if you want to see more rows
# print(Heart, width=1000)   # use this if you want to see more columns

Heart       # shows you a bit of data with some info.

class(Heart)               # it's tibble

is.data.frame(Heart)       # tibble is also a dataframe.

names(Heart)               # all the column names of the tibble


##------------------------------
## 2. You can make DF a Tibble

iris                    # this is built-in data

class(iris)             # it’s data.frame
is.data.frame(iris)
is_tibble(iris)         # not a tibble


library(tidyverse)
iris = as_tibble(iris)  # turn it into a tibble
is.tibble(iris)         # now it’s a tibble
is.data.frame(iris)     # it’s still a data.frame

# Note the difference in how it is shown on screen
iris


##------------------------------
## 3. Select a column in Tibble

Heart            # this is a tibble

Heart["index"]   # select column with name “index”
Heart[1]         # select 1st column (note that there’s no comma)
Heart[ ,1]       # select 1st column (same as the case of matrix)
Heart[1, ]       # select 1st row (same as the case of matrix)
Heart$index      # another way

## You can also use pipe operator “%>%”
## and function select to do the same
Heart %>% select(index)     # select column with name index
Heart %>% select("index")   # quotes are optional


##------------------------------
## 4. Pipe operator

## Make a table of ChestPain column in Heart data
table(Heart["ChestPain"])                # old school way
table(Heart$ChestPain)                   # also an old school way

# Use pipe operator and write
Heart %>% select(ChestPain) %>% table    # new school


##------------------------------
## 5. Check Class

# Method 1
# Apply class() to each column of Heart.  Answer is a list, and hard to read.
Heart %>% lapply(class)

# make the answer not list
Heart %>% lapply(class) %>% unlist

# Method 2
# this is a good way to check class if you can see all the columns
Heart

# If the columns are too many
Heart[1:7]
Heart[8:15]

# Method 3
# This will show all the columns and their class
glimpse(Heart)


##------------------------------
## 6. Rename Column Names

# rename a column
Heart %>% rename(Cpain = ChestPain) %>%
              rename(ix_new = 1)         # rename 1st col


##------------------------------
## 7. Change order of Columns


# Bring a column to 1st
Heart %>% relocate(AHD)

# use .before or .after to specify
Heart %>% relocate(AHD,    .after=Age) %>%
        relocate(MaxHR, .before=Age)


##------------------------------
## 8. NA handling

library(tidyverse)

# load fake sample data
Exam1 = read_csv("https://nmimoto.github.io/datasets/examsSample.csv")
Exam1
names(Exam1)

# see how many NA is in the data
sum(is.na(Exam1))

# number of NA for each column
Exam1 %>% lapply(function(x) sum(is.na(x))) %>% unlist

# number of NA for one column
Exam1 %>% select(Final) %>% is.na %>% sum


##------------------------------
## 9. View Dataframe as Spreadsheet

# view(Heart)     # opens spreadsheet like viewer (not recommended)


##------------------------------
## 10. Export Dataframe

write.csv(Heart, file="temp.csv")

library(writexl)  # install.packages("writexl")
write_xlsx(Heart, "temp.xlsx")

save(Heart, file="temp.Rdata")