This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

plot(cars)

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

#Creating a vector

subject_name <- c("John Doe", "Jane Doe", "Steve Graves")
temperature <- c(98.1, 98.6, 101.4)
flu_status <- c(FALSE, FALSE ,TRUE)
temperature[2] #finding second value in temperature vector
## [1] 98.6
temperature[2:3] #colon gives range for vecotr
## [1]  98.6 101.4
temperature[-2] #negative sign excludes the item
## [1]  98.1 101.4
temperature[c(TRUE,TRUE,FALSE)] #Shows whether to include results
## [1] 98.1 98.6

#Factors

gender <- factor(c("MALE", "FEMALE", "MALE")) #adding a gender factor
gender
## [1] MALE   FEMALE MALE  
## Levels: FEMALE MALE
blood <- factor(c("O", "AB", "A"), #blood type factor
              levels= c("A", "B", "AB", "O"))
blood
## [1] O  AB A 
## Levels: A B AB O
symptoms <- factor(c("SEVERE", "MILD", "MODERATE"), #adding an ordered factor
                   levels = c("MILD", "MODERATE", "SEVERE"),
                   ordered = TRUE)
symptoms
## [1] SEVERE   MILD     MODERATE
## Levels: MILD < MODERATE < SEVERE
symptoms > "MODERATE" #shows if they are greater than moderate
## [1]  TRUE FALSE FALSE

#Lists

subject_name[1] #shows patient name
## [1] "John Doe"
temperature[1]
## [1] 98.1
flu_status[1]
## [1] FALSE
gender[1]
## [1] MALE
## Levels: FEMALE MALE
blood[1]
## [1] O
## Levels: A B AB O
symptoms[1]
## [1] SEVERE
## Levels: MILD < MODERATE < SEVERE
subject1 <- list(fullname = subject_name[1], #making a list for patient 1
                 temperature = temperature[1],
                 flu_status = flu_status[1],
                 gender = gender[1],
                 blood = blood[1],
                 symptoms = symptoms[1])
subject1
## $fullname
## [1] "John Doe"
## 
## $temperature
## [1] 98.1
## 
## $flu_status
## [1] FALSE
## 
## $gender
## [1] MALE
## Levels: FEMALE MALE
## 
## $blood
## [1] O
## Levels: A B AB O
## 
## $symptoms
## [1] SEVERE
## Levels: MILD < MODERATE < SEVERE
subject1[2] #returns second line result of list, sublist
## $temperature
## [1] 98.1
subject1[[2]] #returns a numeric version of second row of list
## [1] 98.1
subject1$temperature #returns result from temperature line of list
## [1] 98.1
subject1[c("temperature", "flu_status")] #gives multiple results from list
## $temperature
## [1] 98.1
## 
## $flu_status
## [1] FALSE
subject1[2:3] #gets second and third line of list
## $temperature
## [1] 98.1
## 
## $flu_status
## [1] FALSE

#data frames

pt_data <- data.frame(subject_name, temperature, flu_status, gender, blood, symptoms, stringsAsFactors = FALSE) #making data frame

pt_data
pt_data$subject_name #returns all info from subject name column of data frame
## [1] "John Doe"     "Jane Doe"     "Steve Graves"
pt_data[c("temperature", "flu_status")] #getting multiple columns
pt_data[2:3] #same way to get above info
pt_data[1,2] #gets info from first row, second column
## [1] 98.1
pt_data[c(1,3), c(2,4)] #multiple rows and columns
pt_data[,1] #shows all rows of column 1
## [1] "John Doe"     "Jane Doe"     "Steve Graves"
pt_data[1,] #shows all columns of row 1
pt_data[,] #all rows and columns
pt_data[c(1,3), c("temperature", "gender")]
pt_data[-2, c(-1,-3,-5,-6)] #same way to access above info
pt_data$temp_c <- (pt_data$temperature - 32) *(5/9) #making celcuis temp column
pt_data[c("temperature", "temp_c")] #shows both F and C temps

#Matrixies

m <- matrix(c(1,2,3,4), nrow = 2) #makes 2x2 matrix
m
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
m <- matrix(c(1,2,3,4), ncol = 2) #makes 2x2 matrix in same way
m
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
m <- matrix(c(1,2,3,4,5,6), ncol = 2) #makes 3x2 matrix
m
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
m[1,1] #shows value from row and column 1 in matrix
## [1] 1
m[3,2] #3rd row, 2nd column
## [1] 6
m[1,] #shows value from row 1
## [1] 1 4
m[,1] #values of column 1
## [1] 1 2 3