plot(cars)

# create vectors of data for three medical patients
subject_name <- c("John Doe", "Marcus", "Steve Graves")
temperature <- c(98.1, 98.6, 101.4)
flu_status <- c(FALSE, FALSE, TRUE)
#Enter the second element in body temperature vector
temperature[2]
## [1] 98.6
temperature[1:2]
## [1] 98.1 98.6
#exclude item 2 using the minus sign
temperature[-2]
## [1]  98.1 101.4
#Use a vector to indicate whether to include item
temperature[c(FALSE,TRUE,TRUE)]
## [1]  98.6 101.4

FACTOR

#add gender
gender <- factor(c("MALE","FEMALE","MALE"))
gender
## [1] MALE   FEMALE MALE  
## Levels: FEMALE MALE
#add blood type
blood <- factor (c("O","AB","A"),
                  levels= c("A","B","AB","O"))
blood
## [1] O  AB A 
## Levels: A B AB O
# add ordered factor
symptoms <- factor(c("SEVERE","MILD","MODERATE"),
                   levels = c("MILD","MODERATE","SEVERE"),ordered = TRUE)
symptoms
## [1] SEVERE   MILD     MODERATE
## Levels: MILD < MODERATE < SEVERE
#check for symptoms greater than mild
symptoms > "MILD"
## [1]  TRUE FALSE  TRUE

LISTS

subject_name[2]
## [1] "Marcus"
temperature[2]
## [1] 98.6
flu_status[2]
## [1] FALSE
gender[2]
## [1] FEMALE
## Levels: FEMALE MALE
# create list for a patient
subject2 <- list(fullname = subject_name[2], 
                 temperature = temperature[2],
                 flu_status = flu_status[2],
                 gender = gender[2],
                 blood = blood[2],
                 symptoms = symptoms[2])
subject2
## $fullname
## [1] "Marcus"
## 
## $temperature
## [1] 98.6
## 
## $flu_status
## [1] FALSE
## 
## $gender
## [1] FEMALE
## Levels: FEMALE MALE
## 
## $blood
## [1] AB
## Levels: A B AB O
## 
## $symptoms
## [1] MILD
## Levels: MILD < MODERATE < SEVERE

Methods for accessing a list

subject2[c("temperature", "flu_status")]
## $temperature
## [1] 98.6
## 
## $flu_status
## [1] FALSE
subject2$temperature
## [1] 98.6

Access a list like a vector

# get values 1 and 2
subject2[1:2]
## $fullname
## [1] "Marcus"
## 
## $temperature
## [1] 98.6

Create a data frame from medical patient data

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

#Accessing a data frame

#get a single column
pt_data$subject_name
## [1] "John Doe"     "Marcus"       "Steve Graves"
# extracting temperature and flu_status

pt_data[c("temperature","flu_status")]
#or

#pt_data[2:3]
# accessing several rows and several columns using vectors
pt_data[c(1, 3), c(2, 4)]

Leave a row or column blank to extract all rows or columns

# row 1, all columns
pt_data[3, ]
# all rows and all columns
pt_data[ , ]
# the following are equivalent
pt_data[c(1, 3), c("temperature", "gender")]
pt_data[-2, c(-1, -3, -5, -6)]
# creating a Celsius temperature column
pt_data$temp_c <- (pt_data$temperature - 32) * (5 / 9)
# comparing before and after
pt_data[c("temperature", "temp_c")]

Matrixes

#a 2x2 matrix
m <- matrix(c(1, 2, 3, 4), nrow = 2)
m
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
# equivalent to the above
m <- matrix(c(1, 2, 3, 4), ncol = 2)
m
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
# 2x3 matrix
m3 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
m3
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
#a 3x2 matrix
m3x2 <- matrix(c(1, 2, 3, 4, 5, 6), ncol = 2)
m3x2
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
#extract values from matrixes
m[1, 1]
## [1] 1
m3x2[3, 2]
## [1] 6
# extract rows
m3x2[1, ]
## [1] 1 4
# extract columns
m[, 1]
## [1] 1 2

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.