R Programming
Atomic vectors key point
Remember that an atomic vector can only contain elements of the same type. If you want to store elements of different types in the same data structure, you can use a list.
Atomic vectors
- 6 primary types of atomic vectors logical, integer, double, character (contains strings), complex and raw, complex and raw aren't as common is data analysis so ignore these - Integer and double vectors are known as numeric vectors because they both contain numbers
Data structures
- A format for organising and storing data, think of them like a house that contain your data - Most common data structures in R programming include: Vectors Data frames Matrices Arrays
Creating vectors
- By using the c() function (combined function), it combines multiple values into a vector - For example, you can use the c() function to store numeric data in a vector. c(2.5, 48.5, 101.5) - To create a vector of integers using the c() function, you must place the letter "L" directly after each number. c(1L, 5L, 15L) - You can also create a vector containing characters or logicals c("Sara" , "Lisa" , "Anna") c(TRUE, FALSE, TRUE)
Vectors - Type
- Every vector you create will have two key properties, type and length - You can determine what type of vector you are working with by using the typeof() function. Place the code for the vector inside the parentheses of the function. When you run the function, R will tell you the type. For example character: typeof(c("a" , "b")) #> [1] "character" OR integer: typeof(c(1L , 3L)) #> [1] "integer"
Data frames key points
- First, columns should be named. - Second, data frames can include many different types of data, like numeric, logical, or character. - Finally, elements in the same column should be of the same type.
Data frames
- Most common way of storing and analyzing data in R - A data frame is a collection of columns similar to spreadsheets or SQL table, each column has a name at the top that represents a variable and includes one observation per row - Data frames help summarize data and organize it into a format that is easy to read and use.
Vectors
- Two types: Atomic vectors and lists You can use R code to create these - A vector is a group of data elements of the same type, stored in a sequence in R. You cannot have a vector that contains both logicals and numerics.
Naming Vectors
All types of vectors can be named. Names are useful for writing readable code and describing objects in R. You can name the elements of a vector with the names() function. As an example, let's assign the variable x to a new vector with three elements. x <- c(1, 3, 5) You can use the names() function to assign a different name to each element of the vector. names(x) <- c("a", "b", "c") Now, when you run the code, R shows that the first element of the vector is named a, the second b, and the third c. x #> a b c #> 1 3 5
Vector - Specific type
You can also check if a vector is a specific type by using an is function: is.logical(), is.double(), is.integer(), is.character(). In this example, R returns a value of TRUE because the vector contains integers. x <- c(2L, 5L, 11L) is.integer(x) #> [1] TRUE In this example, R returns a value of FALSE because the vector does not contain characters, rather it contains logicals. y <- c(TRUE, TRUE, FALSE) is.character(y) #> [1] FALSE
Vectors Length
You can determine the length of an existing vector-meaning the number of elements it contains-by using the length() function. In this example, we use an assignment operator to assign the vector to the variable x. Then, we apply the length() function to the variable. When we run the function, R tells us the length is 3. x <- c(33.5, 57.75, 120.05) length(x) #> [1] 3