R Markdown

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

scurvy <- medicaldata::scurvy

# now print the columns for id and treatment
scurvy %>% 
  select(study_id:treatment) |>  #this selects columns
  slice_tail(n = 7) #slices last seven rows
## # A tibble: 7 × 2
##   study_id treatment        
##   <chr>    <fct>            
## 1 006      vinegar          
## 2 007      sea_water        
## 3 008      sea_water        
## 4 009      citrus           
## 5 010      citrus           
## 6 011      purgative_mixture
## 7 012      purgative_mixture

example

medicaldata::cytomegalovirus %>% 
  head()

reading data

url_stem <- "https://raw.githubusercontent.com/higgi13425/rmrwr-book/master/"
url_stem
## [1] "https://raw.githubusercontent.com/higgi13425/rmrwr-book/master/"
read_csv(glue(url_stem,'data/scurvy.csv'))
## Rows: 12 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): study_id, treatment, dosing_regimen_for_scurvy, gum_rot_d6, skin_so...
## 
## ℹ 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.
## # A tibble: 12 × 8
##    study_id treatment            dosing_regimen_for_s…¹ gum_rot_d6 skin_sores_d6
##    <chr>    <chr>                <chr>                  <chr>      <chr>        
##  1 001      cider                1 quart per day        2_moderate 2_moderate   
##  2 002      cider                1 quart per day        2_moderate 1_mild       
##  3 003      dilute_sulfuric_acid 25 drops of elixir of… 1_mild     3_severe     
##  4 004      dilute_sulfuric_acid 25 drops of elixir of… 2_moderate 3_severe     
##  5 005      vinegar              two spoonfuls, three … 3_severe   3_severe     
##  6 006      vinegar              two spoonfuls, three … 3_severe   3_severe     
##  7 007      sea_water            half pint daily        3_severe   3_severe     
##  8 008      sea_water            half pint daily        3_severe   3_severe     
##  9 009      citrus               two lemons and an ora… 1_mild     1_mild       
## 10 010      citrus               two lemons and an ora… 0_none     0_none       
## 11 011      purgative_mixture    a nutmeg-sized paste … 3_severe   3_severe     
## 12 012      purgative_mixture    a nutmeg-sized paste … 3_severe   3_severe     
## # ℹ abbreviated name: ¹​dosing_regimen_for_scurvy
## # ℹ 3 more variables: weakness_of_the_knees_d6 <chr>, lassitude_d6 <chr>,
## #   fit_for_duty_d6 <chr>

saving data

scurvy_data <- read_csv(glue(url_stem, 'data/scurvy.csv'))
## Rows: 12 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): study_id, treatment, dosing_regimen_for_scurvy, gum_rot_d6, skin_so...
## 
## ℹ 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.

making column names with a 13*8 tibble

read_csv(file = glue(url_stem, 'data/scurvy.csv'), 
col_names = c("pat_id", "arm", "dose", "gums", "skin", "weak", "lass", "fit"))
## Rows: 13 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): pat_id, arm, dose, gums, skin, weak, lass, fit
## 
## ℹ 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.
## # A tibble: 13 × 8
##    pat_id   arm                  dose              gums  skin  weak  lass  fit  
##    <chr>    <chr>                <chr>             <chr> <chr> <chr> <chr> <chr>
##  1 study_id treatment            dosing_regimen_f… gum_… skin… weak… lass… fit_…
##  2 001      cider                1 quart per day   2_mo… 2_mo… 2_mo… 2_mo… 0_no 
##  3 002      cider                1 quart per day   2_mo… 1_mi… 2_mo… 3_se… 0_no 
##  4 003      dilute_sulfuric_acid 25 drops of elix… 1_mi… 3_se… 3_se… 3_se… 0_no 
##  5 004      dilute_sulfuric_acid 25 drops of elix… 2_mo… 3_se… 3_se… 3_se… 0_no 
##  6 005      vinegar              two spoonfuls, t… 3_se… 3_se… 3_se… 3_se… 0_no 
##  7 006      vinegar              two spoonfuls, t… 3_se… 3_se… 3_se… 3_se… 0_no 
##  8 007      sea_water            half pint daily   3_se… 3_se… 3_se… 3_se… 0_no 
##  9 008      sea_water            half pint daily   3_se… 3_se… 3_se… 3_se… 0_no 
## 10 009      citrus               two lemons and a… 1_mi… 1_mi… 0_no… 1_mi… 0_no 
## 11 010      citrus               two lemons and a… 0_no… 0_no… 0_no… 0_no… 1_yes
## 12 011      purgative_mixture    a nutmeg-sized p… 3_se… 3_se… 3_se… 3_se… 0_no 
## 13 012      purgative_mixture    a nutmeg-sized p… 3_se… 3_se… 3_se… 3_se… 0_no

making column names with a 12*8 tibble

read_csv(file = glue(url_stem, 'data/scurvy.csv'), 
         col_names = c("pat_id", "arm", "dose", "gums", "skin", "weak", "lass", "fit"),
         skip = 1)
## Rows: 12 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): pat_id, arm, dose, gums, skin, weak, lass, fit
## 
## ℹ 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.
## # A tibble: 12 × 8
##    pat_id arm                  dose                gums  skin  weak  lass  fit  
##    <chr>  <chr>                <chr>               <chr> <chr> <chr> <chr> <chr>
##  1 001    cider                1 quart per day     2_mo… 2_mo… 2_mo… 2_mo… 0_no 
##  2 002    cider                1 quart per day     2_mo… 1_mi… 2_mo… 3_se… 0_no 
##  3 003    dilute_sulfuric_acid 25 drops of elixir… 1_mi… 3_se… 3_se… 3_se… 0_no 
##  4 004    dilute_sulfuric_acid 25 drops of elixir… 2_mo… 3_se… 3_se… 3_se… 0_no 
##  5 005    vinegar              two spoonfuls, thr… 3_se… 3_se… 3_se… 3_se… 0_no 
##  6 006    vinegar              two spoonfuls, thr… 3_se… 3_se… 3_se… 3_se… 0_no 
##  7 007    sea_water            half pint daily     3_se… 3_se… 3_se… 3_se… 0_no 
##  8 008    sea_water            half pint daily     3_se… 3_se… 3_se… 3_se… 0_no 
##  9 009    citrus               two lemons and an … 1_mi… 1_mi… 0_no… 1_mi… 0_no 
## 10 010    citrus               two lemons and an … 0_no… 0_no… 0_no… 0_no… 1_yes
## 11 011    purgative_mixture    a nutmeg-sized pas… 3_se… 3_se… 3_se… 3_se… 0_no 
## 12 012    purgative_mixture    a nutmeg-sized pas… 3_se… 3_se… 3_se… 3_se… 0_no

find col characters

spec(scurvy_data)
## cols(
##   study_id = col_character(),
##   treatment = col_character(),
##   dosing_regimen_for_scurvy = col_character(),
##   gum_rot_d6 = col_character(),
##   skin_sores_d6 = col_character(),
##   weakness_of_the_knees_d6 = col_character(),
##   lassitude_d6 = col_character(),
##   fit_for_duty_d6 = col_character()
## )

changing data type

scurvy_cols <- read_csv(
  file = glue(url_stem, 'data/scurvy.csv'),
    col_types = cols(
  study_id = col_integer(),
  treatment = col_factor(),
  dosing_regimen_for_scurvy = col_character(),
  gum_rot_d6 = col_character(),
  skin_sores_d6 = col_character(),
  weakness_of_the_knees_d6 = col_character(),
  lassitude_d6 = col_character(),
  fit_for_duty_d6 = col_character()
)
)
glimpse(scurvy_cols)
## Rows: 12
## Columns: 8
## $ study_id                  <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
## $ treatment                 <fct> cider, cider, dilute_sulfuric_acid, dilute_s…
## $ dosing_regimen_for_scurvy <chr> "1 quart per day", "1 quart per day", "25 dr…
## $ gum_rot_d6                <chr> "2_moderate", "2_moderate", "1_mild", "2_mod…
## $ skin_sores_d6             <chr> "2_moderate", "1_mild", "3_severe", "3_sever…
## $ weakness_of_the_knees_d6  <chr> "2_moderate", "2_moderate", "3_severe", "3_s…
## $ lassitude_d6              <chr> "2_moderate", "3_severe", "3_severe", "3_sev…
## $ fit_for_duty_d6           <chr> "0_no", "0_no", "0_no", "0_no", "0_no", "0_n…

changing datatype simply

scurvy_cols2 <- read_csv(
  file = glue(url_stem, 'data/scurvy.csv'),
         col_types = "ifcffff")

glimpse(scurvy_cols2)
## Rows: 12
## Columns: 8
## $ study_id                  <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
## $ treatment                 <fct> cider, cider, dilute_sulfuric_acid, dilute_s…
## $ dosing_regimen_for_scurvy <chr> "1 quart per day", "1 quart per day", "25 dr…
## $ gum_rot_d6                <fct> 2_moderate, 2_moderate, 1_mild, 2_moderate, …
## $ skin_sores_d6             <fct> 2_moderate, 1_mild, 3_severe, 3_severe, 3_se…
## $ weakness_of_the_knees_d6  <fct> 2_moderate, 2_moderate, 3_severe, 3_severe, …
## $ lassitude_d6              <fct> 2_moderate, 3_severe, 3_severe, 3_severe, 3_…
## $ fit_for_duty_d6           <chr> "0_no", "0_no", "0_no", "0_no", "0_no", "0_n…