Vectors in R Programming Language

The post is about another data structure called Vectors in R Programming. It is in the form of questions and answers with examples. Here we will discuss some important vector functions, recycling of elements, and different types of vectors with examples.

What are Vectors in R Programming?

Vectors in R Programming are basic data structures. It comes in two parts: atomic vectors and lists (recursive vectors). A vector in R language is a fundamental data structure that stores a collection of elements, all of the same data type (like numbers, characters, or logical values). Vectors in R Programming are essentially one-dimensional arrays.

How many types of vectors are in R?

The primary types of vectors in R Programming are

  • Logical Vectors (stores TRUE or FALSE values)
  • Integer Vectors (Stores Whole numbers, i.e., integers only)
  • Double (Numeric) Vectors (Stores decimal numbers)
  • Character Vectors (Stores text strings)

The less common types of vectors are:

  • Complex Vectors
  • Raw Vectors.

How to Create Vectors in R Programming Language?

To create vectors in R Programming Language, the following are few ways:

  • Create a vector using integers, use the colon (:) operator. For Example, typing 2:6 results in a vector with numbers from 2 to 6, and typing 3:-4 creates a vector with the numbers 3 to -4.
  • Create a vector using the seq() Function, Write a command such as seq(from = 4.5, to = 3.0, by = -0.5) to create a vector of numbers from 4.5 to 3.0 by decrementing 0.5 step, that is, 4.5 4.0 3.5 3.0.
  • The seq() function may also be used by specifying the length of the sequence by using the argument out, e.g., seq(from = -2.7, to = 1.3, length.out = 9). It will result in -2.7 -2.2 -1.7 -1.2 -0.7 -0.2 0.3 0.8 1.3.

What are Logical Vectors in R Programming?

In R language, a logical vector contains elements having the values TRUE, FALSE, and NA. Like numerical vectors, R allows the manipulation of logical quantities.

What are Vector Functions?

In R language, some functions are used to perform some computation or operation on vector objects, for example, rep(), seq(), all(), any(), and c(), etc. However, the most common functions that are used in different vector operations are rep(), seq(), and c() functions.

How One Can Repeat Vectors in R?

One can use the rep() function to repeat the vectors. For example, to repeat a vector: c(0, 0, 7), three times, one can use rep(c(0, 0, 7), times = 3).

To repeat a vector several times, each argument can be used, for example, rep(c(2, 4, 2), each = 2).

To repeat each element, and how often it has to repeat, one can use the code, rep(c(0, 0, 7), times = 5)

The length.out argument can be used to repeat the vector until it reaches that length, even if the last repetition is incomplete. For example, rep(1:3, length.out = 9)

rep(c(0, 0, 7), times = 3)

rep(c(2, 4, 2), each = 2)
rep(c(0, 0, 7), times = 5)
rep(1:3, length.out = 9)
Vectors in R Programming Language

What is the Recycling of Elements in R Vectors?

When two vectors of different lengths are involved in an operation then the elements of the shorter vector are reused to complete the operation. This is called the recycling of elements in R vectors. For example,

v1 <- c(4, 1, 0, 6)
v2 <- c(2, 4)
v1 * v2

## Output
8, 4, 0, 24

In the above example, the elements 2 and 4 are repeated.

What do copy-on-change Issues in R?

It is an important feature of R that makes it safer to work with data. Let us create a numeric vector x1 and assign the values of x1 to x2.

x1 <- c(1, 2, 3, 4)
x2 <- x1

Now x1 and x2 vectors have exactly the same values. If one modifies the element(s) in one of the two vectors, the question is do both vectors change?

x1[1] <- 0
x1
## Output
0 2 3 4

x2

## Output
1 2 3 4

The output shows that when x1 is changed, the vector x2 will remain unchanged. It means that the assignment automatically copies the values and makes the new variable point to the copy of the data instead of the original data.

Basic Computer MCQs

Control Structures in R

The post is about different control structures in R Language. In R there are different control structures to control the flow or execution of the program. The control function usually makes use of if statements or their different flavors to control the code based on condition(s). On the other hand, one can repeat some desired task in sequence or based on conditions such as repeated sampling or simulation can be performed using loops such as for loop.

Control Structures in R Language

What is a Control Structure in R?

R language has some standard control structures. Many R expressions/ statements (or code blocks) can be enclosed within braces { }. Control structures define the flow of the program. However, it is more efficient to use built-in functions rather than control structures whenever possible. These allow us to control the flow of execution of a script typically inside of a function.

How many Control Statements are in R Language?

There are eight control statements in R language.

Name all of the Control Statements in R

The names of the control statements in R language are:

  • if
  • if-else
  • for
  • nested loops
  • while
  • repeat and break
  • next
  • return

In general, one can say that there are two types of control structures.

  • Conditional statements (if, if-else, elif, and switch statement)
  • Loops (for loop, while loop, and repeat)

What is “if” control statement in R Language?

The if statements are used when a certain condition is TRUE to perform a specific task. The syntax of an if statement is

if (test expression or condition){
  statement(s)
}

How if statements work, consider the following a simple example

x <- 1:20
if (sample(x, 1) <= 10){
  print("Sampled x is less than 10")
}

From the above example, if the randomly sampled value is less than 10, then the output of the code will be “Sampled x is less than 10”, otherwise nothing will be displayed on the screen as no else is utilized here.

What is a loop in R Language?

A loop is a way to repeat a sequence of instructions/commands under certain conditions. The loops allow us to automate parts of our code that require repetition.

What does mean by the term “Dreaded for Loop”?

In R language, many questions arose about how to accomplish different tasks without the use of a for loop. This is a situation of “Dreaded for Loop”. It is usually desired as programmers try to avoid loops at all costs to speed up their code.

Give an Example of “Dreaded for Loop”

The simplest example of “Dreaded for loop” is the use of vectorization. The vectorization in R speeds up some computation, compared to the use of loops. For example, if x and y are vectors of equal lengths, one can add/sum them as

# Vectorization
x <- 1:5
y <- 5:1
z <- x + y
print(z)


# Using for loop
for (i in length(x)){
 z[i] <- x[i] + y[i]
}
print(z)
Control Structures in R Language

The addition of element-wise values of the $x$ and $y$ vectors is much faster than adding/summing using loops. Note that the use of vectorization also helps the programmers to write shorter, simpler, safer, and faster code.

What is the Purpose of Using the next statement in R?

One can use the next statement if, in a loop, the programmer/ developer wants to skip the current iteration without terminating it loop.

Statistics and Data Analysis

MCQs Shiny App Quiz Questions 21

The post is about Shiny app Quiz Questions. There are 20 multiple-choice questions about R Language and Shiny App. Let us start with MCQs Shiny App Quiz Questions with Answers now.

MCQs Shiny App Quiz Questions

Online MCQs Shiny App Quiz Questions

1. Which function adds a title to a fluidPage layout?

 
 
 
 

2. What are the two main differences between an R Markdown document and a Shiny dashboard? MCQs in Statistics

 
 
 
 

3. Which statement best describes the varSelectInput() function?

 
 
 
 

4. You must include the shinyApp() function in the code for all Shiny apps.

 
 
 
 

5. Which function creates an empty layout?

 
 
 
 

6. Can you publish a Shiny app to your shinyapps.io account from RStudio?

 
 
 
 

7. In a Shiny application, where do you add input widgets?

 
 
 
 

8. which function is used to construct an initial empty UI when creating a Shiny app?

 
 
 
 

9. A Shiny app consists of two parts, the server that the user interacts with and the UI that powers the app.

 
 

10. You use the Layout functions to organize —————- containing user interface elements in the application.

 
 
 
 

11. Which deployment method should you select for your Shiny app if you do not want to run your server? Computer MCQs Test

 
 
 
 

12. When defining the server logic for a Shiny app, you define a function that includes which of the following parameters?

 
 
 
 

13. Which is the preferred method for creating a Shiny app?

 
 
 

14. Which two components of a dashboard happen on the back end?

 
 
 
 

15. Which of the following is a true statement about functions found in the Shiny library?

 
 
 
 

16. Which two components of a dashboard happen on the front end?

 
 
 
 

17. Which of the following is NOT a parameter to the varSelectInput() function?

 
 
 
 

18. If you have the command plotOutput(“plot_histogram”) in the UI-side code in your Shiny application, what is the name of the variable that you assign the plot to in the server-side code?

 
 
 
 

Online MCQs Shiny App Quiz Questions

  • Which two components of a dashboard happen on the back end?
  • Which is the preferred method for creating a Shiny app?
  • You must include the shinyApp() function in the code for all Shiny apps.
  • Which function creates an empty layout?
  • If you have the command plotOutput(“plot_histogram”) in the UI-side code in your Shiny application, what is the name of the variable that you assign the plot to in the server-side code?
  • Can you publish a Shiny app to your shinyapps.io account from RStudio?
  • In a Shiny application, where do you add input widgets?
  • Which deployment method should you select for your Shiny app if you do not want to run your server? Computer MCQs Test
  • What are the two main differences between an R Markdown document and a Shiny dashboard? MCQs in Statistics
  • A Shiny app consists of two parts, the server that the user interacts with and the UI that powers the app.
  • Which two components of a dashboard happen on the front end?
  • You use the Layout functions to organize —————- containing user interface elements in the application.
  • When defining the server logic for a Shiny app, you define a function that includes which of the following parameters?
  • Which of the following is a true statement about functions found in the Shiny library?
  • which function is used to construct an initial empty UI when creating a Shiny app?
  • Which of the following is NOT a parameter to the varSelectInput() function?
  • Which function adds a title to a fluidPage layout?
  • Which statement best describes the varSelectInput() function?

MCQs in Statistics