1. What is R Programming?
R is an open-source programming language widely used for statistical analysis, graphical representation, reporting, machine learning, and data analysis. R is an interpreted language, and its core functionalities are built using R itself. However, it also supports integration with C, C++, and Fortran for performance-intensive operations.
2. What are the advantages of using R language?
- Excellent for statistical analysis, machine learning, data visualization, and reporting.
- Interpreted language and platform-independent.
- Open-source and highly extensible with thousands of CRAN packages.
- Strong community support with frequent updates and enhancements.
- Supports vectorized operations which make it powerful for numerical computations.
3. What is a factor in R programming?
Factors are used to handle categorical data in R. They store data as a set of predefined levels (unique values). Factors can store both strings and integers and are especially useful in statistical modeling.
4. Why is R programming important in Big Data?
R is valuable in big data due to its extensive ecosystem of packages such as dplyr, data.table, and sparklyr that help in data wrangling, modeling, and visualization. Libraries like ggplot2 and plotly help visualize large datasets effectively.
5. How to calculate the execution time of a program in R?
You can calculate execution time using:
start_time <- Sys.time()
# your code here
end_time <- Sys.time()
execution_time <- end_time - start_time
print(execution_time)
Or use the tictoc package:
library(tictoc)
tic()
# your code
toc()
6. How to take input in R programming?
Use the readline() function to take user input interactively:
user_input <- readline(prompt = "Enter your name: ")
print(paste("Hello", user_input))
7. What is a vector in R programming?
A vector is a basic data structure in R that holds elements of the same data type. You can create vectors using the c() function:
vec <- c(1, 2, 3, 4)
typeof(vec) # To check data type
length(vec) # To check length
8. What is an array in R programming?
An array in R can store data in more than two dimensions (multi-dimensional). It holds only a single data type. Use the array() function to create it.
9. What is a matrix in R programming?
A matrix is a 2D data structure where elements are arranged in rows and columns. You can create a matrix using the matrix() function.
10. How are commands written in R?
Commands can be executed interactively in the R console or stored in an R script (.R file) and run using:
$ Rscript your_script.R
11. How many data structures are there in R?
- Vector
- List
- Matrix
- Array
- Factor
- Data Frame
12. What are the ways to call a function in R?
You can call a function by simply using its name and passing required arguments:
sum(1, 2) # Function call
13. How do you use R to make a histogram?
Use the hist() function:
data <- c(1, 2, 3, 4, 5, 6, 7)
hist(data)
14. What is the 'next' statement in R?
The next statement is used to skip the current iteration in a loop and proceed to the next one:
for (i in 1:5) {
if (i == 3) next
print(i)
}
15. What are various packages in R?
Packages in R contain functions, data, and documentation. Examples include:
- ggplot2
- dplyr
- caret
- shiny
- tidyr
16. Where are R packages stored?
R packages are stored in the library directory. You can find your current library path using:
.libPaths()
17. What is the difference between R and Python?
Aspect | R | Python |
---|---|---|
Primary Use | Statistical analysis, visualization | General-purpose, AI, web, data science |
Learning Curve | Steeper for beginners | More beginner-friendly |
Popular Users | Statisticians, Data Scientists | Developers, Data Scientists, Engineers |
Package Repository | CRAN | PyPI |
Visualization | ggplot2, lattice | matplotlib, seaborn |
18. How to delete a column in R?
To delete a column in a data frame:
df$column_name <- NULL
Other methods:
- Using subset(): subset(df, select = -column_name)
- By index: df <- df[ , -c(2)]
19. How to write a function in R?
You can define a function using the function keyword:
add <- function(x, y) {
result <- x + y
return(result)
}
print(add(3, 5))
20. What is the difference between a Data Frame and a Matrix in R?
Data Frame: Can contain different data types in each column (numeric, character, factor, etc.).
Matrix: Can only contain one type of data (all numeric, or all character).
21. How do you install and load a package in R?
install.packages("ggplot2") # Install
library(ggplot2) # Load
22. What is the apply() function family in R?
The apply() family includes:
- apply() – used for matrices and arrays
- lapply() – returns a list
- sapply() – simplifies output to vector/matrix
- tapply() – applies a function over subsets of a vector
23. What is the purpose of the aggregate() function in R?
aggregate() is used to summarize data by applying a function to subsets of a data frame based on one or more grouping variables.
aggregate(Sepal.Length ~ Species, data = iris, FUN = mean)
24. How to handle missing values in R?
You can use:
- is.na() – to detect missing values
- na.omit() – to remove rows with missing data
- mean(x, na.rm = TRUE) – to ignore NA while computing mean
25. What is the difference between == and %in% in R?
== compares values element by element.
%in% checks if values exist in a vector.
5 %in% c(1, 2, 5) # TRUE
5 == c(1, 2, 5) # FALSE FALSE TRUE
26. What is the purpose of the with() function in R?
with() is used to simplify referencing variables in a data frame without repeating the data frame name.
with(mtcars, mean(mpg))
27. How do you merge two data frames in R?
Use the merge() function:
merge(df1, df2, by = "id")
28. What is the pipe operator (%>%) in R?
Introduced by the magrittr package (and now in base R as |>), the pipe operator passes the output of one function to the next.
library(dplyr)
mtcars %>%
filter(mpg > 20) %>%
summarise(mean_hp = mean(hp))
29. How to write conditional statements in R?
R supports if, else if, else statements, and ifelse() for vectorized conditions.
x <- 10
if (x > 0) {
print("Positive")
} else {
print("Non-positive")
}