Data Camp: Intro to R Flashcards
vector is it array?
Vectors are one-dimension arrays that can hold numeric data, character data, or logical data
A cheat on how to find those element of vector > 0
# Poker and roulette winnings from Monday to Friday: poker_vector 0
# Select from poker_vector these days poker_winning_days
Why matrix is called 2 dimentional?
Since you are only working with rows and columns, a matrix is called two-dimensional.
You can construct a matrix in R with the matrix() function. Consider the following example:
how to define matrix and its contents?
matrix(1:9, byrow = TRUE, nrow = 3)
In the matrix() function:
The first argument is the collection of elements that R will arrange into the rows and columns of the matrix. Here, we use 1:9 which is a shortcut for c(1, 2, 3, 4, 5, 6, 7, 8, 9).
The argument byrow indicates that the matrix is filled by the rows. If we want the matrix to be filled by the columns, we just place byrow = FALSE.
The third argument nrow indicates that the matrix should have three rows.
How to name matrix headings?
Similar to vectors, you can add names for the rows and the columns of a matrix
rownames(my_matrix)
Which function to calculate rowsum?
In R, the function rowSums() conveniently calculates the totals for each row of a matrix. This function creates a new vector:
How can you combine 2 vectors?
rbind(x,y)
How to calculate all the colum in matrix?
colSums(all_wars_matrix)
How to do matrix selection? or in all colum or in all rows?
my_matrix[1,2] selects the element at the first row and second column.
my_matrix[1:3,2:4] results in a matrix with the data on the rows 1, 2, 3 and columns 2, 3, 4.
If you want to select all elements of a row or a column, no number is needed before or after the comma, respectively:
my_matrix[,1] selects all elements of the first column.
my_matrix[1,] selects all elements of the first row.
Example of how factor() diff nominal or ordinal categorical variable?
# Animals > animals_vector factor_animals_vector factor_animals_vector [1] Elephant Giraffe Donkey Horse Levels: Donkey Elephant Giraffe Horse > > # Temperature > temperature_vector factor_temperature_vector factor_temperature_vector [1] High Low High Low Medium Levels: Low < Medium < High
If you don’t specify the levels of the factor when creating the vector, R will automatically assign them alphabetically
How to see exact content of a vector wiith string even with without escape xters ?
use writeLine()
how to show first and last part of ibservation in R?
So how to do this in R? Well, the function head() enables you to show the first observations of a data frame. Similarly, the function tail() prints out the last observations in your data set.
Both head() and tail() print a top line called the ‘header’, which contains the names of the different variables in your data set.
How do u find the structure of ur dataframe? for example number of variable , observations , variable names , data types of each variable?
str(). The function str() shows you the structure of your data set. For a data frame it tells you:
The total number of observations (e.g. 32 car types)
The total number of variables (e.g. 11 car features)
A full list of the variables names (e.g. mpg, cyl … )
The data type of each variable (e.g. num)
The first observations
Whe you first recieved a data frame , what d u need to do to see the llok of the data and its contents?
Applying the str() function will often be the first thing that you do when receiving a new data set or data frame. It is a great way to get more insight in your data set before diving into the real analysis.
How to create dataframe?
planets_df