Basic data type: Double String Logic
If the items are not of the same type, they are coerced:
string <-- numeric <-- logical
negative indexes (used for exclusion)
Automatic vector extension makes vectors very different from matrices and arrays, for which automatic extension does not exist.
Lists are sequences of anything. They can contain:
basic data types, vectors, matrices, arrays, lists (recursion),…
Create a vector consisting of the numbers 1 to N, where 1 appears once, 2 appears twice, 3 appears 3 times,…
rep(1:x, 1:x)
Reshape vector into a matrix in two ways (arranged by row and column)
matrix(0:1, nrow=3, ncol=4, byrow=T) # if want to arrange by column, just without the "byrow" part
[,1] [,2] [,3] [,4]
[1,] 0 1 0 1
[2,] 0 1 0 1
[3,] 0 1 0 1
Select rows of a matrix in R that meet a condition
car_models <- ``c``(``'Maruti'``,``'Hyundai'``,``'Tata'``,
``'Ford'``,``'Nissan'``,``'Toyota'``)
car_type <- ``c``(``'Diesel'``,``'Petrol'``,``'Petrol'``,
``'Diesel'``,``'Petrol'``,``'Diesel'``)
car_color <- ``c``(``'Red'``,``'Blue'``,``'Red'``,
``'Red'``,``'Blue'``,``'Red'``)
year <- ``c``(2001,2011,2013,2012,2021,2021)
# Storing matrix in mat (variable)
mat <- ``cbind``(car_models,car_type,car_color,year)
# condition to select only rows with
# color = Red
mat <- mat[mat[,``"car_color"``]==``"Red"``,]
# displaying the resultant matrix
mat
car_models car_type car_color year
[1,] "Maruti" "Diesel" "Red" "2001"
[2,] "Tata" "Petrol" "Red" "2013"
[3,] "Ford" "Diesel" "Red" "2012"
[4,] "Toyota" "Diesel" "Red" "2021"