Podcast
Questions and Answers
Which of the following best describes a data frame in R?
Which of the following best describes a data frame in R?
- A generic data object used for storing tabular data, similar to a spreadsheet or SQL table. (correct)
- A specialized type of list that can only hold numeric data.
- A statistical model for performing regression analysis.
- A function used to create plots and charts from data.
Given the following R code:
vec1 = c(1, 2, 3)
vec2 = c("R", "Scilab", "Java")
df = data.frame(vec1, vec2)
What is the structure of the resulting df
?
Given the following R code:
vec1 = c(1, 2, 3)
vec2 = c("R", "Scilab", "Java")
df = data.frame(vec1, vec2)
What is the structure of the resulting df
?
- A single vector containing all the values from `vec1` and `vec2`.
- A data frame with two columns: `vec1` (numeric) and `vec2` (character). (correct)
- A list with two elements, `vec1` and `vec2`, each containing their original values.
- A matrix with two columns, one containing numeric values and the other containing character strings .
When creating a data frame from a file, which separator should be used in the read.table()
function if the data entries are separated by commas?
When creating a data frame from a file, which separator should be used in the read.table()
function if the data entries are separated by commas?
- `sep = "|"`
- `sep = ","` (correct)
- `sep = " "`
- `sep = "\t"`
In R, what does df[1:2,]
refer to when accessing elements of a data frame df
?
In R, what does df[1:2,]
refer to when accessing elements of a data frame df
?
What will be the output of the following R code?
pd <- data.frame(
Name = c("Senthil","Senthil","Sam","Sam"),
BS = c(141.2,139.3,135.2,160.1)
)
pd2 <- subset(pd, Name == "Senthil" | BS > 150)
print(pd2)
What will be the output of the following R code?
pd <- data.frame(
Name = c("Senthil","Senthil","Sam","Sam"),
BS = c(141.2,139.3,135.2,160.1)
)
pd2 <- subset(pd, Name == "Senthil" | BS > 150)
print(pd2)
Given a data frame df
, what is the purpose of the following R code?
df[[2]][2] <- "NewValue"
Given a data frame df
, what is the purpose of the following R code?
df[[2]][2] <- "NewValue"
Which R command allows you to interactively edit a dataframe in a spreadsheet-like interface?
Which R command allows you to interactively edit a dataframe in a spreadsheet-like interface?
What functions can be used, respectively, to add extra rows and extra columns to a data frame in R?
What functions can be used, respectively, to add extra rows and extra columns to a data frame in R?
Given a data frame df
, what does the following code do?
df2 <- df[-3, -1]
Given a data frame df
, what does the following code do?
df2 <- df[-3, -1]
If you want to remove the column named "Salary"
from a data frame called employees
, which of the following R code snippets would achieve this?
If you want to remove the column named "Salary"
from a data frame called employees
, which of the following R code snippets would achieve this?
What is the primary issue that arises when manipulating character columns in a data frame after its creation, and how can it be resolved?
What is the primary issue that arises when manipulating character columns in a data frame after its creation, and how can it be resolved?
In R, if you encounter NA
values after assigning a character string to a factor column in a dataframe, what is the most likely cause?
In R, if you encounter NA
values after assigning a character string to a factor column in a dataframe, what is the most likely cause?
How can we ensure the dataframe does not automatically convert the strings into factors?
How can we ensure the dataframe does not automatically convert the strings into factors?
Given the code:
df <- data.frame(col1 = c(1, 2, 3), col2 = c("A", "B", "C"), stringsAsFactors = TRUE)
df[2, 2] <- "D"
print(df)
What will print(df)
output?
Given the code:
df <- data.frame(col1 = c(1, 2, 3), col2 = c("A", "B", "C"), stringsAsFactors = TRUE)
df[2, 2] <- "D"
print(df)
What will print(df)
output?
With stringsAsFactors = FALSE
, what is the output of the following code?
df <- data.frame(col1 = c(1, 2, 3), col2 = c("A", "B", "C"), stringsAsFactors = FALSE)
df[2, 2] <- "D"
print(df)
With stringsAsFactors = FALSE
, what is the output of the following code?
df <- data.frame(col1 = c(1, 2, 3), col2 = c("A", "B", "C"), stringsAsFactors = FALSE)
df[2, 2] <- "D"
print(df)
Flashcards
What are Data Frames?
What are Data Frames?
Generic data objects in R used to store tabular data.
What does read.table() do?
What does read.table() do?
Reads data from a file to create a dataframe
What is subset()?
What is subset()?
Extracts a subset of data based on specified conditions.
What does df[val1, val2] refer to?
What does df[val1, val2] refer to?
Signup and view all the flashcards
What does rbind() do?
What does rbind() do?
Signup and view all the flashcards
What does cbind() do?
What does cbind() do?
Signup and view all the flashcards
What is the edit() function used for?
What is the edit() function used for?
Signup and view all the flashcards
What does df[-3,-1] do?
What does df[-3,-1] do?
Signup and view all the flashcards
What does stringsAsFactors = F do?
What does stringsAsFactors = F do?
Signup and view all the flashcards
Study Notes
- Data frames are generic data objects of R, used for storing tabular data.
Creating Data Frames
- Data frames can be created by reading data from a file
- Use '/' instead of '' for file paths
- Example file path: "C:/Users/hii/Documents/R/R-Workspace/"
- A separator can distinguish between entries; the default separator is space.
- Command to read a file:
newDF = read.table(path="Path of the file")
- Including a separator:
newDF = read.table(file="path of the file", sep)
Accessing Rows and Columns
df[val1,val2]
refers to row “val1” and column “val2”, which can be number or string- "val1" or "val2" can be an array of values like "1:2" or "c(1,3)"
df[val2]
(no commas) refers to column “val2” only
Subsetting
- The
subset()
function extracts a subset of data based on conditions. - Example:
pd=data.frame("Name"=c("Senthil","Senthil","Sam","Sam"), "Month"=c("Jan","Feb","Jan","Feb"), "BS" = c(141.2,139.3,135.2,160.1), "BP" = c(90,78,80,81))
pd2 = subset(pd,Name=="Senthil" | BS> 150 )
Editing
- Data frames can be edited by direct assignment
- A data frame can be edited using the
edit()
command - Create an instance of a data frame and use the
edit
command to manually open a table editor to change values
Adding Rows and Columns
- Extra rows can be added with the function
rbind
- Extra columns can be added with the function
cbind
- Example:
df = rbind(df,data.frame(vec1=4, vec2="C", vec3="For Scaleup"))
df = cbind(df,vec4=c(10,20,30,40))
Deleting Rows and Columns
- Rows and columns can be deleted in multiple ways
- A '-' sign before the value and before ',' deletes rows
- A '-' sign before the value and after ',' deletes columns
!
means no to specified rows/columns satisfying the condition
Manipulating Rows - Factor Issue
- Character columns become factors when created in a data frame
- Factor variables are character columns split into categories or factor levels
- When character columns are created in a data frame, they become factors
- New entries are to be consistent with factor levels fixed when the dataframe is first created
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.