Sample code in R

Author

JPS

Reading a file in R

Let’s use an example from class to read a file.

The code below read a phenotype file

library(lme4)
Loading required package: Matrix
library(lmerTest)

Attaching package: 'lmerTest'
The following object is masked from 'package:lme4':

    lmer
The following object is masked from 'package:stats':

    step
library(tidyverse)
── Attaching packages
───────────────────────────────────────
tidyverse 1.3.2 ──
✔ ggplot2 3.4.0      ✔ purrr   1.0.0 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.5.0 
✔ readr   2.1.3      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ tidyr::expand() masks Matrix::expand()
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
✖ tidyr::pack()   masks Matrix::pack()
✖ tidyr::unpack() masks Matrix::unpack()
library(pedigreemm)
library(regress)
setwd("C:/Users/marti/OneDrive/Documents/job/562")

rm(list=ls())
ls()
character(0)
getwd()
[1] "C:/Users/marti/OneDrive/Documents/job/562"
list.files()
 [1] "02-Lab.pdf"                                                                          
 [2] "02 Lab  solved.qmd"                                                                  
 [3] "02 Lab.html"                                                                         
 [4] "02 Lab.qmd"                                                                          
 [5] "05 - INTRO LMM.html"                                                                 
 [6] "05 - INTRO LMM.qmd"                                                                  
 [7] "08---Animal-Models_files"                                                            
 [8] "08 - Animal Models.html"                                                             
 [9] "08 - Animal Models.qmd"                                                              
[10] "08 - Animal Models_files"                                                            
[11] "09 - Solving linear equations.html"                                                  
[12] "09 - Solving linear equations.qmd"                                                   
[13] "09 - Solving linear equations_files"                                                 
[14] "2988185.pdf"                                                                         
[15] "562"                                                                                 
[16] "978-1-0716-2460-9.pdf"                                                               
[17] "Amat.RDS"                                                                            
[18] "AN S 562 Syllabus - Spring 2022.docx"                                                
[19] "AN S 562 Syllabus - Spring 2022.pdf"                                                 
[20] "example INTRO.R"                                                                     
[21] "Fixed effects models.pptx"                                                           
[22] "Henderson.pdf"                                                                       
[23] "INTRO-FELM_files"                                                                    
[24] "INTRO FELM.html"                                                                     
[25] "INTRO FELM.pptx"                                                                     
[26] "INTRO FELM.qmd"                                                                      
[27] "INTRO FELM_files"                                                                    
[28] "INTRO.pptx"                                                                          
[29] "Lab 02 - Solved.R"                                                                   
[30] "Lab 02.R"                                                                            
[31] "Lab 03.R"                                                                            
[32] "Lab 03_solved.R"                                                                     
[33] "lab 05.R"                                                                            
[34] "lab 05_solved.R"                                                                     
[35] "Lab 06_solved.R"                                                                     
[36] "Linear Models and Animal Breeding Lawrence R. Schaeffer Centre  ( PDFDrive ) (1).pdf"
[37] "PAG_GWA.pptx"                                                                        
[38] "ped.txt"                                                                             
[39] "pheno_ready.txt"                                                                     
[40] "PM Timesheet PP-2.pdf"                                                               
[41] "RR_trend.jpg"                                                                        
[42] "rsconnect"                                                                           
[43] "Schedule 562.xlsx"                                                                   
[44] "test.html"                                                                           
[45] "test.qmd"                                                                            
[46] "test.rmarkdown"                                                                      
[47] "test_files"                                                                          
[48] "ThatBLUP.pdf"                                                                        
[49] "weights_OLS.jpg"                                                                     
[50] "weights_OLS_QR.jpg"                                                                  
[51] "weights_OLS_QR_H.jpg"                                                                
[52] "weights_QR.jpg"                                                                      
pheno<-read.table("pheno_ready.txt",sep="\t",header = T)

The next step is to do some summaries

Phenotypic distribution of birthweight

hist(pheno$wt_birth,main="birth weight in Kg")

boxplot(pheno$wt_birth~pheno$sex)