
R Programming Interview Questions and Answers
Top 100 R Programming Programming Interview Questions for Freshers
R Programming is an essential skill for data analysts, statisticians, and machine learning professionals. It enables users to analyze, visualize, and manipulate data efficiently, making it a preferred choice for statistical computing and data-driven applications. Mastering R Programming allows professionals to build robust data models, perform statistical analysis, and create interactive data visualizations.Candidates should be well-prepared to tackle both the R Programming Online Assessment and Technical Interview Round at IDM TechPark.
To help you succeed, we have compiled a comprehensive list of the Top 100 R Programming Interview Questions along with their answers. Mastering these concepts will give you a competitive edge in securing a role in data analytics, machine learning, and data science.
1. What is R programming?
Answer:
R is an open-source programming language primarily used for statistical computing, data analysis, and visualization. It provides extensive libraries for data science and machine learning.
2. How do you install a package in R?
Answer:
Use the install.packages() function:
install.packages("ggplot2")
3. How do you load a package in R?
Answer:
Use the library() function:
library(ggplot2)
4. How do you check the version of R installed on your system?
Answer:
Use the version or R.version.string command:
R.version.string
5. How do you create a vector in R?
Answer:
Using the c() function:
x <- c(1, 2, 3, 4, 5)
6. What are the data types in R?
Answer:
-
Numeric
-
Integer
-
Character
-
Logical
-
Factor
-
Complex
7. How do you check the data type of a variable in R?
Answer:
Use the class() function:
class(10) # Output: "numeric"
8. How do you create a matrix in R?
Answer:
Use the matrix() function:
mat <- matrix(1:6, nrow=2, ncol=3)
9. How do you create a data frame in R?
Answer:
Using the data.frame() function:
df <- data.frame(Name = c("Alice", "Bob"), Age = c(25, 30))
10. How do you access elements in a vector?
Answer:
Using indexing:
x[2] # Access the second element
11. What is the difference between == and = in R?
Answer:
-
== is used for comparison.
-
= is used for assigning values inside function arguments.
Example:
x <- 10 # Assignment x == 10 # Comparison
12. What is the difference between NA, NULL, and NaN in R?
Answer:
-
NA (Not Available): Missing values in data.
-
NULL: Represents an empty object.
-
NaN (Not a Number): Result of undefined mathematical operations (e.g., 0/0).
Example:
is.na(NA) # TRUE is.null(NULL) # TRUE is.nan(NaN) # TRUE
13. How do you generate a sequence of numbers in R?
Answer:
Using the seq() function:
seq(1, 10, by=2) # Output: 1, 3, 5, 7, 9
14. What function is used to apply an operation to each element of a vector?
Answer:
The sapply() or lapply() function:
sapply(c(1, 2, 3), sqrt) # Computes the square root
15. How do you read a CSV file in R?
Answer:
Using the read.csv() function:
df <- read.csv("data.csv")
16. How do you write a CSV file in R?
Answer:
Using the write.csv() function:
write.csv(df, "output.csv", row.names=FALSE)
17. What is the difference between paste() and paste0() in R?
Answer:
-
paste() adds a separator (default is space).
-
paste0() concatenates without spaces.
Example:
paste("Hello", "World") # "Hello World" paste0("Hello", "World") # "HelloWorld"
18. How do you remove missing values (NA) from a dataset?
Answer:
Using na.omit():
clean_data <- na.omit(df)
19. How do you check the structure of a dataset in R?
Answer:
Using the str() function:
str(df)
20. How do you find the number of rows and columns in a dataset?
Answer:
Using nrow() and ncol():
nrow(df) # Number of rows ncol(df) # Number of columns
21. How do you rename columns in R?
Answer:
Using colnames():
colnames(df) <- c("NewCol1", "NewCol2")
22. What is the difference between apply(), lapply(), and sapply()?
Answer:
FunctionWorks onReturns
apply()Matrices & Data FramesArray or vector
lapply()Lists & VectorsList
sapply()Lists & VectorsSimplified output
Example:
apply(matrix(1:6, nrow=2), 1, sum) # Sum by row lapply(1:3, sqrt) # Square root for each element sapply(1:3, sqrt) # Simplified output
23. How do you subset data in R?
Answer:
Using indexing:
df[df$Age > 25, ] # Select rows where Age > 25
24. What is the difference between a list and a vector in R?
Answer:
-
A vector contains elements of the same type.
-
A list can store elements of different types.
Example:
v <- c(1, 2, 3) # Vector l <- list(1, "Hello", TRUE) # List
25. How do you plot a graph in R?
Answer:
Using the plot() function:
plot(1:10, 1:10, type="l", col="blue") # Line plot