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)
# create vectors of data for three medical patients
presidents_name <- c("Jan Trump", "Roberto Carlos", "Juan Manuel")
presidents_salary <- c(98000, 95000, 100000)
application_status <- c(FALSE, FALSE, TRUE)
# access the second element in body president_name vector
presidents_name[2]
[1] "Roberto Carlos"
## [1] 98.6
# include items in the range 2 to 3
presidents_salary[2:3]
[1] 95000 100000
# exclude item 2 using the minus sign
presidents_name[-2]
[1] "Jan Trump" "Juan Manuel"
# use a vector to indicate whether to include item
application_status[c(TRUE, TRUE, FALSE)]
[1] FALSE FALSE
#FACTORS
# add governemnt expendature factor
government_expendature <- factor(c("LOW", "MODERATE", "HIGH"),
levels = c("LOW", "MODERATE", "HIGH"),
ordered = TRUE)
government_expendature
[1] LOW MODERATE HIGH
Levels: LOW < MODERATE < HIGH
#Check for lower government_expendature than moderate
government_expendature < "MODERATE"
[1] TRUE FALSE FALSE
presidents_salary[1]
[1] 98000
presidents_name[1]
[1] "Jan Trump"
application_status[1]
[1] FALSE
government_expendature [1]
[1] LOW
Levels: LOW < MODERATE < HIGH
#create a list for the presidency term
subject1 <- list(presidents_name = presidents_name[1], presidents_salary = presidents_salary[1],application_staus = application_status[1],
government_expendature = government_expendature[1])
subject1
$presidents_name
[1] "Jan Trump"
$presidents_salary
[1] 98000
$application_staus
[1] FALSE
$government_expendature
[1] LOW
Levels: LOW < MODERATE < HIGH
pt_data <- data.frame(presidents_name, presidents_salary, application_status, government_expendature, stringsAsFactors = FALSE)
# display the data frame
pt_data
# get a single column
pt_data$subject_name
NULL
# get several columns by specifying a vector of names
pt_data[c("presidents_name", "presidents_salary")]
#this is the same as above, extracting application_status and government_status
pt_data[1:2]
pt_data$presidents_name
[1] "Jan Trump" "Roberto Carlos"
[3] "Juan Manuel"
pt_data[(presidents_name),(government_expendature)]
pt_data[2:3] #just showing column 2 and 3
#accessing by row and column
pt_data[2,2]
[1] 95000
#accessing several rows and columns
pt_data [c(2,1),c(3,1)]
m1<-matrix(c(1,2,3,4),nrow=2)
m1
[,1] [,2]
[1,] 1 3
[2,] 2 4
m2<-matrix(c(1,2,3,4),nrow=2)
m2
[,1] [,2]
[1,] 1 3
[2,] 2 4
#extract first row first column
m1[1,1]
[1] 1
#extract second row ferst columns of m2
m2[1,2]
[1] 3
#creating a 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
m4<-matrix(c(2,3,7,9,11,10i),ncol = 2)
m4
[,1] [,2]
[1,] 2+0i 9+ 0i
[2,] 3+0i 11+ 0i
[3,] 7+0i 0+10i
#extract first row
m4[1,]
[1] 2+0i 9+0i
#extract every row in the second column
m4[,2]
[1] 9+ 0i 11+ 0i 0+10i