Interview Questions on R.

1. What is R?
R is an open-source programming language used for statistical computing and data visualization.
2. How do you install a package in R?
install.packages("package_name")
3. How do you load a package in R?
library(package_name)
4. What are the different data types in R?
Numeric, Integer, Character, Logical, Complex, and Factor.
5. What is a data frame in R?
A data frame is a table-like structure with rows and columns, similar to a spreadsheet.
6. How do you create a vector in R?
c(1, 2, 3, 4, 5)
7. How do you check the structure of a dataset in R?
str(dataset)
8. How do you subset data in R?
dataset[1:5, ] or subset(dataset, condition)
9. What is the difference between == and = in R?
= is used for assignment, while == is used for comparison.
10. How do you merge two data frames in R?
merge(df1, df2, by="column_name")
11. What is the apply family of functions in R?
Functions like apply(), lapply(), sapply() that apply operations over data structures.
12. How do you handle missing values in R?
na.omit(df) or is.na(df)
13. How do you plot a graph in R?
plot(x, y) or ggplot2 for advanced plotting.
14. What is the difference between a list and a vector in R?
A list can hold multiple data types, while a vector holds elements of the same type.
15. How do you convert a factor to a numeric variable?
as.numeric(as.character(factor_variable))
16. What is tapply() in R?
tapply() applies a function over subsets of a vector.
17. How do you perform linear regression in R?
lm(y ~ x, data=df)
18. What is the difference between aggregate() and group_by()?
aggregate() is a base R function, while group_by() is from dplyr and provides more flexibility.
19. How do you read a CSV file in R?
read.csv("file.csv")
20. How do you write data to a CSV file in R?
write.csv(df, "file.csv")
21. What is the difference between seq() and rep() in R?
seq() generates sequences, rep() repeats elements.
22. How do you remove duplicate rows from a dataset?
unique(df)
23. What is a factor in R?
A factor is a categorical variable with levels.
24. How do you create a bar plot in R?
barplot(table(df$column))
25. How do you normalize data in R?
(x - min(x)) / (max(x) - min(x))
26. How do you concatenate strings in R?
paste("Hello", "World", sep=" ")
27. How do you apply a function to each row in a data frame?
apply(df, 1, function_name)
28. How do you perform hierarchical clustering in R?
hclust(dist(df))
29. How do you generate random numbers in R?
runif(10) or rnorm(10)
30. What is the difference between matrix and data frame in R?
A matrix contains elements of the same type, while a data frame can contain multiple types.
31. How do you perform decision tree analysis in R?
library(rpart); model <- rpart(y ~ x, data=df)
32. What is cross-validation in R?
Cross-validation is a technique to evaluate model performance by splitting data into training and testing sets.
33. What is the difference between S3 and S4 objects in R?
S3 is simpler and uses list-based object-oriented programming, while S4 is more structured and uses formal class definitions.
34. How do you visualize missing data in R?
Use library(VIM); aggr(df)
35. How do you implement XGBoost in R?
library(xgboost); xgb.train(params, data, nrounds)
36. How do you perform text mining in R?
Use tm and text2vec packages for text processing.
37. How do you create a heatmap in R?
heatmap(matrix_data)
38. What is Monte Carlo simulation in R?
Monte Carlo simulation uses repeated random sampling to model uncertainty.
39. What is the purpose of caret package in R?
Caret provides functions for machine learning model training and validation.
40. How do you create interactive visualizations in R?
Use shiny or plotly packages.
41. How do you save and load R objects?
save(object, file="data.RData") and load("data.RData")
42. What is bootstrapping in R?
Bootstrapping is a resampling technique used for statistical inference.
43. How do you write custom functions in R?
my_function <- function(x) { return(x^2) }
44. What is a lambda function in R?
Anonymous functions created using function(x) x + 1
45. How do you perform survival analysis in R?
Use the survival package and coxph() function.
46. How do you optimize hyperparameters in R?
Use caret or tune packages.
47. What is the difference between factor and character in R?
Factor is used for categorical variables, while character is plain text data.
48. How do you implement neural networks in R?
Use neuralnet or keras packages.
49. What is the purpose of magrittr package in R?
Magrittr provides the pipe operator %>% for cleaner code.
50. How do you parallelize computations in R?
Use parallel or foreach packages.