GWU_Rating <- c(100, 98, 76, 61, 88, 90, 76, 83)
GWU_Rating[1][1] 100
GWU_Rating[4][1] 61
Welcome to the Intermediate R Tutorial webpage! This tutorial will cover more complex topics within R like indexing and slicing, logical operators, and an example user-defined function cover loop and if-else logical statement.
The basic syntax for accessing an element is object[index].
Indexing in R starts at 1 instead of 0.
The syntax for excluding an element is object[-index].
The syntax for accessing X and Y element is object[c(X,Y)].
The syntax for accessing from the Xth element to the Yth element is object[x:y].
The syntax for logical indexing is object[logical statement].
You can either directly access by using the name of a column or by the number of columns.
The number goes from left to right for columns, so the left-most column is column 1.
Names Score Pass_Class
1 Abby 90 TRUE
2 Bob 87 TRUE
3 Chris 67 FALSE
4 Denny 91 TRUE
Score
1 90
2 87
3 67
4 91
Score Pass_Class
1 90 TRUE
2 87 TRUE
3 67 FALSE
4 91 TRUE
The syntax is [row number , column number] For example, here we are fetching the first row and the first two columns in this dataframe.
Subset() is a built-in function in base R. You need to provide the input dataframe and parameters to indicate which part of the dataframe you want to keep. Here is an example for selecting the columns Names and Score.
Here is another example for using a logical statement to only select students that passed the class.
The equal operator returns TRUE when both elements are equal to each other, otherwise it returns FALSE.
The not equal operator returns FALSE when both elements are equal to each other, otherwise it returns TRUE.
The greater than operator returns TRUE when the first element is greater than the second element, otherwise it returns FALSE.
The smaller than operator returns TRUE when the first element is smaller than the second element, otherwise it returns FALSE.
The greater than operator returns TRUE when the first element is greater than or equal to the second element, otherwise it returns FALSE.
The smaller than operator returns TRUE when the first element is smaller than or equal to the second element, otherwise it returns FALSE.
The and operator only returns TRUE when both elements are true, otherwise it returns FALSE. In this case, x is true and y is false, so the and operator would return:
The or operator only returns FALSE when both elements are false, otherwise it returns FALSE. In this case, x is true and y is false, so the or operator would return:
This function includes a for loop and if-else statement. It loops through a vector and calculates the percentage of students that passed the test.
pass_percentage <- function(scores, passing_score = 70) {
pass_count <- 0
fail_count <- 0
for(score in scores) {
if(score >= passing_score) {
pass_count <- pass_count + 1
} else {
fail_count <- fail_count + 1
}
}
pass_percent <- (pass_count / length(scores)) * 100
cat("Students who passed the class:", pass_percent, "%\n")
return(list(pass_count = pass_count, fail_count = fail_count, pass_percent = pass_percent))
}
class_scores <- c(60,70,72,89,91,79,90,95,84,87,76,93,73,74,50,85,78,80,95,79,69,77,69,66,81)
pass_percentage(class_scores)Students who passed the class: 80 %
$pass_count
[1] 20
$fail_count
[1] 5
$pass_percent
[1] 80
On this page, you will find some interview questions to practice your intermediate knowledge of R programming. All of the content is based on the R Intermediate tutorial from this page.
Even if you are not preparing for an interview, these questions can help test your knowledge (perhaps for an upcoming exam, certification, or homework assignment!).
Try to actively recall the answers before checking them. This is one of the most effective ways to learn and increase retention!
A. annual.revenue[“sales”]
B. annual.revenue$sales
C. annual.revenue.sales
D. annual.revenue[sales]
B. annual.revenue$sales
C. Numeric
A. 5, 10, 15, 20
A. Numeric, B. Integer
A. The entire data frame with all three countries.
B. Only the first two columns of the data frame.
C. A syntax error because of incorrect indexing.
D. The first two rows of the data frame, showing all columns.
D. The first two rows of the data frame, showing all columns.
country foundingyear population_million
1 USA 1776 340.1
2 Canada 1867 40.1