num_vect<- c(0.5,55,-10,6)
tf<- num_vect<1
tf
#[1] TRUE FALSE TRUE FALSE
logical operators like ‘|’ and ‘&’ can be used
paste()
it basically joins two strings together
As you can see the first argument of paste() is ... which is referred to as an ellipsis or simply dot-dot-dot. The ellipsis allows an indefinite number of arguments to be passed into a function. In the case of paste() any number of strings can be passed as arguments and
paste() will return all of the strings combined into one string.
paste(1:5)
# "1" "2" "3" "4" "5" ← This returns a vector of strings
paste(1:5, collapse = " ")
# "1 2 3 4 5"
paste(c("Apple", "Banana", "Mango"), collapse = ", ")
# "Apple, Banana, Mango"
paste("file", 1, ".csv", sep = "")
# "file1.csv"
paste0("file", 1:3, ".csv")
# "file1.csv" "file2.csv" "file3.csv"
paste(LETTERS, 1:4, sep = "-")
[1] "A-1" "B-2" "C-3" "D-4" "E-1" "F-2" "G-3" "H-4" "I-1" "J-2" "K-3" "L-4"
[13] "M-1" "N-2" "O-3" "P-4" "Q-1" "R-2" "S-3" "T-4" "U-1" "V-2" "W-3" "X-4"
[25] "Y-1" "Z-2"
subsetting vectors (important) (1)
both are rectangular data types
The main difference, as you'll see, is that matrices can only contain a single class of data, while data frames can consist of many different classes of data
self explanatory code showing how a matrix was created
my_vector <- 1:20
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
dim(my_vector) <- c(4,5)
dim(my_vector)
[1] 4 5
my_vector
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
#a matrix is created
my_matrix <- my_vector
and there obviously is a better way to form a matrix so everything above pretty much is useless
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
my_data <- matrix(1:20, nrow= 4, ncol= 5)
now we have a matrix and if we try binding a new vector patients to it with patient names, to name the rows, it does work but it causes all the data values of the matrix to be converted to string because of implicit conversion, to avoid this we use data frames
patients <- c("Bill", "Gina", "Kelly", "Sean")
my_data <- data.frame(patients, my_matrix)
patients X1 X2 X3 X4 X5
1 Bill 1 5 9 13 17
2 Gina 2 6 10 14 18
3 Kelly 3 7 11 15 19
4 Sean 4 8 12 16 20
cnames <- c("patient", "age", "weight", "bp", "rating", "test")
colnames(my_data) <- cnames
my_data
patient age weight bp rating test
1 Bill 1 5 9 13 17
2 Gina 2 6 10 14 18
3 Kelly 3 7 11 15 19
4 Sean 4 8 12 16 20
more stuff about dataframes: