R Language interview questions

R Language quiz questions

  • 1.

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

    Answer:

    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))
    which(mydata$v1==max(mydata$v1))
    It returns 3 as 12 is the maximum value and it is at 3rd row in the variable x=v1.

    View
  • 2.

    How can you merge two data frames in R language?

    Answer:

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

    View
  • 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?

    Answer:

    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.

    View
  • 4.

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

    Answer:

    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.

    View
  • 5.

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

    Answer:

    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)
    cumsum(y)

    Output of the above R code-

    Cumsum(y)

    f m

    3 3

    View
  • 6.

    How to check the frequency distribution of a categorical variable?

    Answer:

    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.

    gender=factor(c(“M”,”F”,”M”,”F”,”F”,”F”))

    table(sex)

    Output of the above R Code –

    Gender

    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
    View
  • 7.

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

    Answer:

    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
    tt[1:depth]
    

    Output -

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

    How will you merge two dataframes in R programming language?

    Answer:

    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.
    View
  • 9.

    What is R Base package?

    Answer:

    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.

    View
  • 10.

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

    Answer:

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

    var2

     It will give an error.

    View
  • 11.

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

    Answer:

    Emp_sal= 2000+2.5(emp_age)2

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

    View
  • 12.

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

    Answer:

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

    View
  • 13.

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

    Answer:

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

    View
  • 14.

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

    Answer:

    mat<-matrix(rep(c(TRUE,FALSE),8),nrow=4)

    sum(mat)

     8

    View
  • 15.

    What will be the output of runif (7)?

    Answer:

    It will generate 7 randowm numbers between 0 and 1.

    View
  • 16.

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

    Answer:

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

    df
    ##   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-

    df1<-df[-c(2,3)]

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

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

    Answer:

    Setwd(“dir_path”)

    View
  • 18.

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

    Answer:

    Hist()

    View
  • 19.

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

    Answer:

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

    View
  • 20.

    Which function helps you perform sorting in R language?

    Answer:

    Order ()

    View

© 2017 QuizBucket.org