# R Programming Interview Questions And Answers

### Top 21 R Programming Interview Questions And Answers

#### 1) What is R or R programming?

R is programming language which is used for statistical analysis, graphic representations and reporting.

R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently being developed by the R Development Core Team. One of the interesting fact is that this programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka).
At very core of R programming language there lies an interpreted computer language which allows branching and looping as well as modular programming.

#### 2) Is R open source or licensed?

R is open source programming language GNU-style copy left, and an official part of the GNU project called GNU S.

#### 3) What are the features of R programming?

Following are the key features of R programming:

● R is simple and effective programming language
● R is effective in data handling and storage
● R provides excellent mechanism for calculation of different matrices.
● R provides variety of tools for data analysis
● R provides graphical facilities for data analysis

#### 4) What is Rscript in R programming?

Rscript is the R interpreter which is used to execute the R program scripts in command prompt.

R program scripts have .R extension. Here is the sample script:

` \$ Rscript test.R `

#### 5) What are the R-Objects in R?

R-Objects are the objects which are assigned to the variables and the data type of the R-object becomes the data type of the variable. Different types of R-Objects are:

● Lists
● Vectors
● Arrays
● Matrices
● Factors
● Data Frames

#### 6) Which is the simplest R-Object in R?

Vector object is the simplest R-Object.

#### 7) Which are the different data types of these atomic vectors in R programming?

There are six data types also termed as classes of vectors. Following are the data types:

● Logical
● Numeric
● Integer
● Complex
● Character
● Raw

Here is the sample code for these six data types:

● Logical:

`val <- FALSE  print(class(val))`

● Numeric:
`val <- 27.5 print(class(val))`

● Integer:
`val <- 21L print(class(val))`

● Complex:
`val <- 27+3i print(class(val))`

● Character:
`val <- "READ" print(class(val))`

● Raw:
`val <- charToRaw("Testfry") print(class(val))`

#### 8) What is c() function in vector in R programming?

C() function is used to combine the elements into a vector.

Sample code:

``` apple <- c('red','green','yellow')
print(apple) ```

#### 9) What are the Matrices in R programming?

Matrix is a two-dimensional data set which can created using matrix function.

Sample code:

```M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)```

#### 10) What are the Factors in R programming?

Factors are the R-Objects that are created using a vector. It stores the vector along with the distinct values of the elements in the vector as labels. Factor() function can be used.

Sample code:

`factor_app <- factor(colors)`

#### 11) What are the Data frames in R programming?

These are the tabular data objects in R programming. Data.frame() function can be used to create data frames.

#### 12) In R programming can a variable name start with dot(.)?

Yes, in R programming variable name can start with dot(.) i.e. it is valid variable name. The only thing that need to be taken into consideration with writing the variable name is that the dot shouldn't followed by the number.

Sample valid variable name:

`.abc`

Sample invalid variable name:
`.3abc`

#### 13) How variable assignment takes place in R programming?

In R programming there are multiple ways to assign the values to the variables. Following are the ways to assign the values:
● Assignment using equal operator
● Assignment using leftward operator
● Assignment using rightward operator

Sample code for assignment using equal operator:

`var.1 = c(0,13,24,344,35)`

Sample code for assignment using leftward operator:
`var.1 <- c("lean","G")`

Sample code for assignment using rightward operator:
`c(TRUE,1) -> var.1`

#### 14) Why R is called as dynamically typed language?

R is called as dynamically typed language which means we can change the data type of same variable again and again in same program.

Sample code:

```var_2 <- "abc"
var_2 <- 14```

#### 15) What is the use of ls() function R programming?

ls() function is use to determine all the variables which are currently available in the workspace.

#### 16) In R programming does ls() function list the variables start with dot?

No, the variables start with dot won't be listed using ls() function.

#### 17) In R programming how we can list the variables start with dot?

The variables start with dot can be listed using "all.names = TRUE" argument to ls() function.

Sample code:

`print(ls(all.name = TRUE))`

#### 18) In R programming how we can delete the variable?

Variables can be deleted using rm() function.

#### 19) In R programming how we can delete all the variable at same time?

We can delete all the variables at same time using rm() and ls() functions.

Sample code:

`rm(list = ls())`

#### 20) What are the different types of operators in R programming?

Following are the different types of operators in R programming:

● Arithmetic Operators
● Assignment Operators
● Logical Operators
● Relational Operators
● Miscellaneous Operators

Following are the different arithmetic operators:

- : Subtraction of two vectors
* : Multiplies both the vectors
/ : Division of the vectors
%% : Give the remainder
^ : Exponential

Following are the different assignment operators:

<- , = , << - : These are the left assignment operators
-> , ->> : These are the right assignment operators

Following are the different logical operators:

&, && : Logical AND operator
|,|| : Logical OR operator
! : Logical NOT operator

Following are the different Relational operators:

> : Greater than logical operator
< : Less than logical operator
== : Equal to logical operator
<= : Less than equal to logical operator
>= : Greater than equal to logical operator
!= : Not equal to logical operator

Following are the different Miscellaneous operators:

: Colon operator. It creates the series of numbers in sequence for a vector.
%in% : This operator is used to identify if an element belongs to a vector.
%*% : This operator is used to multiply a matrix with its transpose.

#### 21) What is the output of below code snippet in R programming:```s <- c( 2,5.5,3) t <- c(4, 3, 4) print(s*t)```

Here is the output:

`[1] 8.0 16.5 12.0`