R Language interview questions

R Language quiz questions

  • 1.

    Explain the usage of which() function in R language.


    which() function determines the postion of elemnts in a logical vector that are TRUE. In the below example, we are finding the row number wherein the maximum value of variable v1 is recorded.

    mydata=data.frame(v1 = c(2,4,12,3,6))
    It returns 3 as 12 is the maximum value and it is at 3rd row in the variable x=v1.

  • 2.

    How can you merge two data frames in R language?


    Data frames in R language can be merged manually using cbind () functions or by using the merge () function on common rows or columns.

  • 3.

    R programming language has several packages for data science which are meant to solve a specific problem, how do you decide which one to use?


    CRAN package repository in R has more than 6000 packages, so a data scientist needs to follow a well-defined process and criteria to select the right one for a specific task. When looking for a package in the CRAN repository a data scientist should list out all the requirements and issues so that an ideal R package can address all those needs and issues.

    The best way to answer this question is to look for an R package that follows good software development principles and practices. For example, you might want to look at the quality documentation and unit tests. The next step is to check out how a particular R package is used and read the reviews posted by other users of the R package. It is important to know if other data scientists or data analysts have been able to solve a similar problem as that of yours. When you in doubt choosing a particular R package, I would always ask for feedback from R community members or other colleagues to ensure that I am making the right choice.

  • 4.

    What will be the result of multiplying two vectors in R having different lengths?


    The multiplication of the two vectors will be performed and the output will be displayed with a warning message like – “Longer object length is not a multiple of shorter object length.” Suppose there is a vector a<-c (1, 2, 3) and vector b <- (2, 3) then the multiplication of the vectors a*b will give the resultant as 2 6 6 with the warning message. The multiplication is performed in a sequential manner but since the length is not same, the first element of the smaller vector b will be multiplied with the last element of the larger vector a.

  • 5.

    What is the procedure to check the cumulative frequency distribution of any categorical variable?


    The cumulative frequency distribution of a categorical variable can be checked using the cumsum () function in R language.

    Example –

    gender = factor(c("f","m","m","f","m","f"))
    y = table(gender)

    Output of the above R code-


    f m

    3 3

  • 6.

    How to check the frequency distribution of a categorical variable?


    The frequency distribution of a categorical variable can be checked using the table function in R language. Table () function calculates the count of each categories of a categorical variable.



    Output of the above R Code –


    F  M

    4  2

    Programmers can also calculate the % of values for each categorical group by storing the output in a dataframe and applying the column percent function as shown below -

    t = data.frame(table(gender))
    t$percent= round(t$Freq / sum(t$Freq)*100,2)

    Gender Frequency Percent
    F 4 66.67
    M 2 33.33
  • 7.

    Write the R programming code for an array of words so that the output is displayed in decreasing frequency order.


    R Programming Code to display output in decreasing frequency order -

    tt <- sort(table(c("a", "b", "a", "a", "b", "c", "a1", "a1", "a1")), dec=T)
    depth <- 3

    Output -

    1) a a1  b
    2) 3  3  2
  • 8.

    How will you merge two dataframes in R programming language?


    Merge () function is used to combine two dataframes and it identifies common rows or columns between the 2 dataframes. Merge () function basically finds the intersection between two different sets of data.

    Merge () function in R language takes a long list of arguments as follows –

    Syntax for using Merge function in R language -

     merge (x, y, by.x, by.y, all.x  or all.y or all )

    • X represents the first dataframe.
    • Y represents the second dataframe.
    • by.X- Variable name in dataframe X that is common in Y.
    • by.Y- Variable name in dataframe Y that is common in X.
    • all.x - It is a logical value that specifies the type of merge. all.X should be set to true, if we want all the observations from dataframe X . This results in Left Join.
    • all.y - It is a logical value that specifies the type of merge. all.y should be set to true , if we want all the observations from dataframe Y . This results in Right Join.
    • all – The default value for this is set to FALSE which means that only matching rows are returned resulting in Inner join. This should be set to true if you want all the observations from dataframe X and Y resulting in Outer join.
  • 9.

    What is R Base package?


    R Base package is the package that is loaded by default whenever R programming environent is loaded .R base package provides basic fucntionalites in R environment like arithmetic calcualtions, input/output.

  • 10.

    What will be the output of the following R programming code ?


    var2<- c("I","Love,"DeZyre")


     It will give an error.

  • 11.

    Can you tell if the equation given below is linear or not ?


    Emp_sal= 2000+2.5(emp_age)2

    Yes it is a linear equation as the coefficients are linear.

  • 12.

    Write a function to extract the first name from the string “Mr. Tom White”.


    substr (“Mr. Tom White”,start=5, stop=7)

  • 13.

    How will you combine multiple different string like “Data”, “Science”, “in” ,“R”, “Programming” as a single string “Data_Science_in_R_Programmming” ?


    paste(“Data”, “Science”, “in” ,“R”, “Programming”,sep="_")

  • 14.

    What will be the output on executing the following R programming code –





  • 15.

    What will be the output of runif (7)?


    It will generate 7 randowm numbers between 0 and 1.

  • 16.

    How will you drop variables using indices in a data frame?


    Let’s take a dataframe df<-data.frame(v1=c(1:5),v2=c(2:6),v3=c(3:7),v4=c(4:8))

    ##   v1 v2 v3 v4
    ## 1  1  2  3  4
    ## 2  2  3  4  5
    ## 3  3  4  5  6
    ## 4  4  5  6  7
    ## 5  5  6  7  8

    Suppose we want to drop variables v2 & v3 , the variables v2 and v3 can be dropped using negative indicies as follows-


    ##   v1 v4
    ## 1  1  4
    ## 2  2  5
    ## 3  3  6
    ## 4  4  7
    ## 5  5  8
  • 17.

    Write the syntax to set the path for current working directory in R environment.



  • 18.

    Which function is used to create a histogram visualisation in R programming language?



  • 19.

    How will you list all the data sets available in all R packages?


    Using the below line of code-
    data(package = .packages(all.available = TRUE))

  • 20.

    Which function helps you perform sorting in R language?


    Order ()


© 2017 QuizBucket.org