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
president_name <- c("Trump", "Biden", "Obama")
president_salary <- c(10, 9, 8)
application_status <- c(FALSE, FALSE, TRUE)
president_name[2]
[1] "Biden"
president_salary[2:3]
[1] 9 8
president_salary[-2]
[1] 10 8
president_salary[c(TRUE, TRUE, FALSE)]
[1] 10 9
gender <- factor(c("MALE", "MALE", "MALE"))
gender
[1] MALE MALE MALE
Levels: MALE
Heigt <- factor(c("5", "6", "7"),
levels = c("5", "6", "7"))
Heigt
[1] 5 6 7
Levels: 5 6 7
skin_color <- factor(c("Orange", "White", "Black"),
levels = c("orange", "white", "black"),
ordered = TRUE)
skin_color > "white"
[1] NA NA NA
president_name[1]
[1] "Trump"
president_salary[1]
[1] 10
application_status[1]
[1] FALSE
Heigt[1]
[1] 5
Levels: 5 6 7
skin_color[1]
[1] <NA>
Levels: orange < white < black
# create list for a president
president_name3 <- list(fullname = president_name[3],
president_salary = president_salary[3],
application_status = application_status[3],
Heigt = Heigt[3],
skin_color = skin_color[3])
president_name
[1] "Trump" "Biden" "Obama"
pt_data <- data.frame(president_name, president_salary, application_status, Heigt,
skin_color, stringsAsFactors = FALSE)
pt_data
#getting a single collum
pt_data$president_name
[1] "Trump" "Biden" "Obama"
pt_data[c("president_salary", "president_name")]
pt_data[2:3]
pt_data[1, 2]
[1] 10
pt_data[c(1, 3), c(2, 4)]
pt_data[, 1]
[1] "Trump" "Biden" "Obama"
pt_data[1, ]
pt_data[ , ]
pt_data[c(1, 3), c("president_name", "president_salary")]
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),ncol = 2)
m2
[,1] [,2]
[1,] 1 3
[2,] 2 4
m1[1,1]
[1] 1
#let us get the second element in first collum of matrix m2
m2[2,1]
[1] 2
#creating a 2x3matrix
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
#lets extract all rows
m4[1,]
[1] 2+0i 9+0i