C1 Intro to R Programming for Data Science Flashcards
(16 cards)
What function can be used to determine the data type in R?
class() function
The class() function returns the class of an R object, allowing you to determine its data type.
Which function checks if a value is an integer?
is.integer() function
The is.integer() function returns TRUE if the value is an integer, otherwise FALSE.
Which function checks if a value is numeric?
is.numeric() function
The is.numeric() function checks if the value is of a numeric type.
Which function checks if a value is a character?
is.character() function
The is.character() function returns TRUE if the value is of character type.
Which function checks if a value is logical?
is.logical() function
The is.logical() function returns TRUE if the value is of logical type.
How can you convert a value to an integer in R?
as.integer() function
The as.integer() function converts a value to an integer type.
How can you convert a value to numeric in R?
as.numeric() function
The as.numeric() function converts a value to numeric type.
How can you convert a value to character in R?
as.character() function
The as.character() function converts a value to character type.
How can you convert a value to logical in R?
as.logical() function
The as.logical() function converts a value to logical type.
Which R function saves a workspace to a .RData file?
Save the entire workspace to a file named “workspace.RData”
save.image(file = “workspace.RData”)
Use Case: This is useful for saving all objects in your environment, ensuring you can resume your work later without losing any data or variables.
Default Behavior: If no file name is specified, save.image() saves the workspace to a file named .RData in the current working directory.
How can you determine the data type of an object in R?
You can use the class() function and the is.integer(), is.numeric(), is.character(), and is.logical() functions to determine the data type.
How can you convert some data types to other data types in R?
You can convert some data types to other data types using the as.integer(), as.numeric(), as.character(), and as.logical() functions.
True or False
The words ‘True’ and ‘False’ are not recognized as boolean values
True
R only recognizes TRUE, FALSE, T and F as special values for true and false. That means all other spellings, including True and true, are not interpreted by R as logical values.
Logical Operations in R
You can do a variety of logical operations in R including:
Checking equivalence: 1 == 2
Checking non-equivalence: TRUE != FALSE
Greater than: 100 > 1
Greater than or equal to: 100 >= 1
Less than: 100 < 1
Less than or equal to: 100 <= 1
Checking NA in R
You can check if a value is NA by using the is.na() function, which returns TRUE or FALSE.
Check if NA: is.na(NA)
Check if not NA: !is.na(2)
How do you convert a vector into a factor?
The function factor() converts a vector into a factor, and creates a factor level for each unique element.
genre_vector <- c(“Comedy”, “Animation”, “Crime”, “Comedy”, “Animation”)
genre_vector
genre_factor <- as.factor(genre_vector)
levels(genre_factor)
‘Animation’ ‘Comedy’ ‘Crime’