Q: How can I load a data set into R?
A: To load a data set into R, you can use the read.csv() function for CSV files or the read.table() function for other types of delimited files.
Here's an example:
# Load a CSV file
my_data <- read.csv("path/to/your/file.csv")
# Load a tab-delimited file
my_data <- read.table("path/to/your/file.txt", sep = "\t", header = TRUE)
Q: How can I view the structure and summary of a data set in R?
A: To view the structure of a data set, you can use the str() function. For a summary of the data set, you can use the summary() function.
Here's an example:
# View the structure of the data set
str(my_data)
# View a summary of the data set
summary(my_data)
Q: How can I access specific columns or rows in a data set in R?
A: To access specific columns in a data set, you can use the $ operator or square brackets []. To access specific rows, you can use square brackets [] with row indices. Here's an example:
# Access a specific column by name using the $ operator
column <- my_data$column_name
# Access a specific column by name using square brackets
column <- my_data["column_name"]
# Access a specific row by index
row <- my_data[3, ]
Q: How can I filter or subset a data set based on specific conditions in R?
A: You can use logical operators and conditions to filter or subset a data set in R.
Here's an example:
# Filter rows where a certain column meets a condition
filtered_data <- my_data[my_data$column_name > 10, ]
# Subset rows based on multiple conditions
subset_data <- my_data[my_data$column1 > 5 & my_data$column2 == "value", ]
Q: How can I add a new column to a data set in R?
A: To add a new column to a data set, you can simply assign values to a new column name using the $ operator.
Here's an example:
# Add a new column with calculated values
my_data$new_column <- my_data$column1 + my_data$column2
# Add a new column with constant values
my_data$new_column <- "value"
Important Interview Questions and Answers on R Data Set
Q: How do you read a CSV file in R and store it as a data frame?
You can use the read.csv() function to read a CSV file and store it as a data frame.
Here's an example:
# Read CSV file and store as a data frame
data <- read.csv("data.csv")
Q: How do you access specific columns in a data frame in R?
You can access specific columns in a data frame using the $ operator or the [, ] indexing.
Here are examples:
# Access column using $
column1 <- data$column_name
# Access column using indexing
column2 <- data[, "column_name"]
Q: How do you filter rows based on a condition in a data frame?
You can use the subset() function to filter rows based on a condition in a data frame.
Here's an example:
# Filter rows based on a condition
filtered_data <- subset(data, column_name > 10)
Q: How do you compute summary statistics for a data frame in R?
You can use the summary() function to compute summary statistics for a data frame.
Here's an example:
# Compute summary statistics
summary_stats <- summary(data)