#R data structures ##Vectors —–

# create vectors of data for four medical patients
subject_name <- c("John Doe", "Jane Doe", "Steve Graves", "Leonel Messi")
temperature <- c(98.1, 98.6, 101.4, 97.9)
flu_status <- c(FALSE, FALSE, TRUE, TRUE)
# access the 4th element in body temperature vector
temperature[4]
## [1] 97.9

Examples of accessing items in vector

# include items in the range 2 to 4
temperature[2:4]
## [1]  98.6 101.4  97.9
# exclude item 3 using the minus sign
temperature[-3]
## [1] 98.1 98.6 97.9
# use a vector to indicate whether to include item
temperature[c(TRUE, FALSE, FALSE, TRUE)]
## [1] 98.1 97.9

Factors —–

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

Lists —–

# display information for a patient
subject_name[4]
## [1] "Leonel Messi"
# display temperature for the 4th patient
temperature[4]
## [1] 97.9
# display flu_status for the 4th patient
flu_status[4]
## [1] TRUE
# display gender for the 4th patient
gender[4]
## [1] UNDENTIFY
## Levels: FEMALE MALE UNDENTIFY
# display blood for the 4th patient
blood[1]
## [1] O
## Levels: A B AB O -A
# display symptoms for the 4th patient
symptoms[4]
## [1] DYING
## Levels: MILD < MODERATE < SEVERE < DYING
# create list for a patient
subject4 <- list(fullname = subject_name[4], 
                 temperature = temperature[4],
                 flu_status = flu_status[4],
                 gender = gender[4],
                 blood = blood[4],
                 symptoms = symptoms[4])
# display the patient
subject4
## $fullname
## [1] "Leonel Messi"
## 
## $temperature
## [1] 97.9
## 
## $flu_status
## [1] TRUE
## 
## $gender
## [1] UNDENTIFY
## Levels: FEMALE MALE UNDENTIFY
## 
## $blood
## [1] -A
## Levels: A B AB O -A
## 
## $symptoms
## [1] DYING
## Levels: MILD < MODERATE < SEVERE < DYING

methods for accessing a list

# get a single list value by position (returns a sub-list)
subject4[2]
## $temperature
## [1] 97.9
# get a single list value by position (returns a numeric vector)
subject4[[2]]
## [1] 97.9
# get a single list value by name
subject4$temperature
## [1] 97.9
# get several list items by specifying a vector of names
subject4[c("temperature", "flu_status")]
## $temperature
## [1] 97.9
## 
## $flu_status
## [1] TRUE

access a list like a vector

# get values 2 and 3
subject4[2:3]
## $temperature
## [1] 97.9
## 
## $flu_status
## [1] TRUE

Data frames —–

#create a data frame from medical patient data
pt_data <- data.frame(subject_name, temperature, flu_status, gender,
                      blood, symptoms, stringsAsFactors = FALSE)
# display the data frame
pt_data

accessing a data frame

# get a single column
pt_data$subject_name
## [1] "John Doe"     "Jane Doe"     "Steve Graves" "Leonel Messi"
# get several columns by specifying a vector of names
pt_data[c("subject_name", "flu_status")]
#Extracting subject_name, temperature, and flu_status
pt_data[1:3]
# accessing by row and column
pt_data[4, 1]
## [1] "Leonel Messi"
# 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

# column 1, all rows
pt_data[, 1]
## [1] "John Doe"     "Jane Doe"     "Steve Graves" "Leonel Messi"
# row 4, all columns
pt_data[4, ]
# 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 —–

# create 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
# create a 2x3 matrix
m3 <- matrix(c(11, 12, 23, 24, 27,30), nrow = 2)
m3
##      [,1] [,2] [,3]
## [1,]   11   23   27
## [2,]   12   24   30
# create a 3x2 matrix
m3A <- matrix(c(11, 12, 23, 24, 27,30), ncol = 2)
m3A
##      [,1] [,2]
## [1,]   11   24
## [2,]   12   27
## [3,]   23   30
# extract values from matrixes
m3A[1, 2]
## [1] 24
m3A[3, 2]
## [1] 30
# extract rows
m3A[1, ]
## [1] 11 24
# extract columns
m3A[, 2]
## [1] 24 27 30