R Programming Basics Quiz
48 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What does the round() function do when applied to a vector?

  • Multiplies each element by 2
  • Rounds each element individually to the nearest integer (correct)
  • Returns the largest element in the vector
  • Calculates the average of the elements

In R, scalars are treated as single-element vectors.

True (A)

What is the result of applying the function round() to the number 1.2?

1

The function f(x, c) returns the value of (x + c) ________.

<p>squared</p> Signup and view all the answers

Match the R function with its description:

<p>round() = Rounds to the nearest integer sqrt() = Calculates the square root f(x,c) = Returns (x+c)^2 recycling = Extending a shorter vector to match a longer one</p> Signup and view all the answers

What happens when you try to call the function f() with a vector as the second argument?

<p>It results in an error (B)</p> Signup and view all the answers

The + operator in R is treated as a function.

<p>True (A)</p> Signup and view all the answers

What is the expected output of the function f(1:3, 1)?

<p>4 9 16</p> Signup and view all the answers

What is the main purpose of the subset() function in R?

<p>To handle NA values automatically (C)</p> Signup and view all the answers

The which() function in R returns the values that satisfy a given condition.

<p>False (B)</p> Signup and view all the answers

What does the filtering expression x[x > 5] return if x contains NA values?

<p>It returns the values greater than 5, including NA where applicable.</p> Signup and view all the answers

The function used to identify the positions within a vector where a condition holds is called _____ function.

<p>which</p> Signup and view all the answers

Match the R operation with its output:

<p>z[c(TRUE,FALSE,TRUE,TRUE)] = Extracts elements based on TRUE/FALSE x[x &gt; 5] = Returns values greater than 5 with NA included subset(x, x &gt; 5) = Returns values greater than 5 without NA which(z*z &gt; 8) = Returns indices of values that satisfy the condition</p> Signup and view all the answers

What does the code first1 <- x[ifelse(x > 6, 2*x, 3*x)] evaluate?

<p>It creates a vector with elements multiplied conditionally (B)</p> Signup and view all the answers

The result of the expression z * z > 8 is a vector of Boolean values.

<p>True (A)</p> Signup and view all the answers

What is the advantage of using ifelse() over traditional if-then-else constructs in R?

<p>It is vectorized and potentially much faster.</p> Signup and view all the answers

What does the expression x[j,] return?

<p>Rows of x specified by the true elements of j (C)</p> Signup and view all the answers

The operation of filtering rows using a Boolean vector is not vectorized.

<p>False (B)</p> Signup and view all the answers

What operator is used for calculating the logical AND of two Boolean vectors in R?

<p>&amp;</p> Signup and view all the answers

The command z <- x[z %% 2 == 1,] extracts rows of x where the elements of z are ___ numbers.

<p>odd</p> Signup and view all the answers

What does the function drop do when applied in data operations?

<p>It retains the two-dimensional nature of data. (D)</p> Signup and view all the answers

Using && will yield the same result as using & for logical operations on vector elements.

<p>False (B)</p> Signup and view all the answers

If we apply the expression m[m[,1] > 1 & m[,2] > 5,], what type of rows are extracted from m?

<p>Rows where the first column is greater than 1 and the second column is greater than 5.</p> Signup and view all the answers

Match the following commands with their functionalities:

<p>j &lt;- x[,2] &gt;= 3 = Identifies which elements in column 2 are at least equal to 3 z &lt;- x[z %% 2 == 1,] = Extracts rows with odd values in z m[m[,1] &gt; 1 &amp; m[,2] &gt; 5,] = Extracts rows based on multiple conditions drop = TRUE = Retains the two-dimensional structure of data</p> Signup and view all the answers

What function is used to add columns to a matrix in R?

<p>cbind() (A)</p> Signup and view all the answers

The rbind() function can be used to add rows to an existing matrix.

<p>True (A)</p> Signup and view all the answers

What happens to the original matrix when you reassign a new matrix to it?

<p>The original matrix is replaced and no longer exists.</p> Signup and view all the answers

Using the cbind() function allows you to bind a column of ______ to the existing matrix.

<p>values</p> Signup and view all the answers

When creating a matrix in a loop, it is more efficient to:

<p>Allocate a large matrix first. (C)</p> Signup and view all the answers

Match the function to its description:

<p>rbind() = Combines rows of matrices cbind() = Combines columns of matrices matrix() = Creates a new matrix dim() = Returns the dimensions of a matrix</p> Signup and view all the answers

What is the time penalty mentioned when repeatedly creating a matrix?

<p>Time taken to allocate memory each time a new matrix is created.</p> Signup and view all the answers

In R, using the function ______() allows for the recycling of values when creating a new matrix.

<p>cbind</p> Signup and view all the answers

What type of data structure is suggested for storing employee information?

<p>List (B)</p> Signup and view all the answers

The numerical indices of list components must always be used when accessing data in a list.

<p>False (B)</p> Signup and view all the answers

What Boolean value indicates that an employee is a member of a union?

<p>TRUE</p> Signup and view all the answers

To access a specific component of a list by name, you can use the syntax j$______.

<p>salary</p> Signup and view all the answers

Which of the following methods can be used to access the salary component of a list named j?

<p>All of the above (D)</p> Signup and view all the answers

Match the following methods of accessing list components with their descriptions:

<p>lst$c = Accessing component by name using dollar sign lst[[&quot;c&quot;]] = Accessing component by name using double brackets lst[[i]] = Accessing component by numerical index lst[&quot;c&quot;] = Accessing component by name returning a sublist</p> Signup and view all the answers

Using single brackets to access a list component returns an element in the original data type.

<p>False (B)</p> Signup and view all the answers

What is the advantage of using names instead of numerical indices in lists?

<p>Clarity and less chance of error</p> Signup and view all the answers

What type of vector is the result of using unlist() on the list containing the values "Joe", "55000", and "TRUE"?

<p>Character vector (D)</p> Signup and view all the answers

The unlist() function will always return a numeric vector if the original list contains numeric values.

<p>False (B)</p> Signup and view all the answers

What function is used to concatenate lists in R, which also includes an option for recursive flattening?

<p>c()</p> Signup and view all the answers

In R, when using the function unlist(), the components of the list are coerced to a common mode, typically resulting in a ____________ vector.

<p>character</p> Signup and view all the answers

Match the following terms with their descriptions:

<p>unlist() = Converts a list to a vector c() = Concatenates objects length() = Returns the number of components recursive = Option for flattening lists during concatenation</p> Signup and view all the answers

What is the result of calling the c() function with the recursive argument set to TRUE on a list where the components themselves are lists?

<p>A single flattened list (C)</p> Signup and view all the answers

When combining lists, the default value for the recursive argument in the c() function is TRUE.

<p>False (B)</p> Signup and view all the answers

What type does R choose when mixing numeric and character types in a list during unlist()?

<p>character</p> Signup and view all the answers

Flashcards

Vectorized functions

Functions that operate on each element of a vector individually, rather than on the entire vector at once.

Recycling in R

R automatically extends shorter vectors to match the length of longer vectors when operating on them, leading to element-wise operations.

Scalar in R

A single numerical value. In R, scalars are treated as vectors of length 1.

Element-wise operations

Operations applied individually to each element of a vector.

Signup and view all the flashcards

Round function

A function that rounds a number to the nearest integer in R.

Signup and view all the flashcards

Built-in Function

A function that is already part of R's core functionality.

Signup and view all the flashcards

Vector Output

A function that operates on a vector returns another vector with the results of the operation applied on each element of the original vector.

Signup and view all the flashcards

Code Safety

Ensuring your code produces the intended results and does not crash by checking data types or lengths in a function.

Signup and view all the flashcards

Filtering Vectors

Selecting elements from a vector based on specified conditions.

Signup and view all the flashcards

subset() Function

A function for filtering vectors that excludes NA values by default.

Signup and view all the flashcards

Basic Filtering

Selects elements of a vector that meet a condition (e.g. x > 5).

Signup and view all the flashcards

NA Handling

How NA (Not Available) values are treated during filtering.

Signup and view all the flashcards

which() Function

Finds the positions (indices) of elements that meet a given condition.

Signup and view all the flashcards

Boolean Vector

A vector containing TRUE or FALSE values.

Signup and view all the flashcards

ifelse() Function

Vectorized function for conditional operations.

Signup and view all the flashcards

Vectorized Operations

Operations performed on all elements of a vector simultaneously.

Signup and view all the flashcards

Boolean Vector j

A vector of TRUE/FALSE values, often the result of a logical comparison; in this case, used for specifying rows in a matrix.

Signup and view all the flashcards

x[j,] operation

Selects specific rows of matrix x using the TRUE/FALSE vector j. It creates a sub matrix of x based on logical indexing based on vector j.

Signup and view all the flashcards

Vectorized Operation

An operation that works on entire vectors or matrices at once, rather than element by element.

Signup and view all the flashcards

Logical Operator (>=)

Compares elements of a vector to a constant or another vector and results in a Boolean vector

Signup and view all the flashcards

Recycling rule

A rule in R where a single value is replicated to match the dimensions of other vectors, crucial in vectorized operations.

Signup and view all the flashcards

Filtering criterion

A condition used to select elements. In this case, elements are selected based on conditions in R vectors.

Signup and view all the flashcards

Vector Boolean AND operator (&)

Combines multiple Boolean vectors element-wise using the AND operation. Useful for combining filtering criteria.

Signup and view all the flashcards

drop=FALSE argument

Forces a result from a subsetting operation to return a matrix (2D structure) rather than a vector (1D).

Signup and view all the flashcards

Reassigning Matrices

Changing a matrix's size or content by creating a new matrix and assigning it to the original variable's name.

Signup and view all the flashcards

cbind()

Function in R used to combine columns of matrices; it creates a new matrix combining columns.

Signup and view all the flashcards

rbind()

Function in R used to combine rows of matrices; it creates a new matrix combining rows.

Signup and view all the flashcards

Matrix resizing

Adding rows or columns to a matrix by creating a new matrix and assigning it to the original variable.

Signup and view all the flashcards

Matrix creation cost

Re-allocating matrix memory is computationally expensive.

Signup and view all the flashcards

Pre-allocate matrix

Creating an empty matrix of the desired size before filling it in parts to avoid repeated costly memory reallocations.

Signup and view all the flashcards

Recycling

R automatically extends shorter vectors/matrices to match the length/size of longer vectors/matrices when performing operations.

Signup and view all the flashcards

Dynamic Memory Allocation

Matrices and vectors in R have dynamic memory allocation - The size can change.

Signup and view all the flashcards

List Indexing in R

Accessing and retrieving specific components within a list using different techniques—component names or numerical indices.

Signup and view all the flashcards

List Components

Individual items(vectors, data types) that make up a list.

Signup and view all the flashcards

List Creation

Generating lists in R using structures like lists of lists or data frames.

Signup and view all the flashcards

List Component Access (names)

Retrieving list components by their designated names.

Signup and view all the flashcards

List Component Access (indices)

Retrieving components by numerical position within a list, treating it like a vector.

Signup and view all the flashcards

Single Bracket Indexing

Extracting a portion of the original list by numerical indices using square brackets.

Signup and view all the flashcards

Double Bracket Indexing

Extracting a single list element by its numerical position. Returns the actual data type contained within the element, no longer a nested list.

Signup and view all the flashcards

List in R

A fundamental data structure in R that can store multiple data types like numeric, character, and logical values within a single structure.

Signup and view all the flashcards

Unlisting a List

Converting a list into a vector, where possible, elements are coerced to a common mode, often resulting in a character vector.

Signup and view all the flashcards

List Coercion

R automatically adjusts data types of list elements when necessary for compatibility during unlisting.

Signup and view all the flashcards

Recursive List

A list containing other lists within it .

Signup and view all the flashcards

Concatenation (c()) with recursive = FALSE

Combines lists without flattening; the result is a recursive structure.

Signup and view all the flashcards

Concatenation (c()) with recursive = TRUE

Combines lists and flattens them, creating one single level list.

Signup and view all the flashcards

List Component Access

Retrieving specific parts of a list; use names(j) to get names, and unlist() to get the values from a list stored in a variable.

Signup and view all the flashcards

List Length

Determining the number of components (sub-lists) in a list using the length() function.

Signup and view all the flashcards

List to Vector Conversion

Transforming a list into a single vector by unlisting its components.

Signup and view all the flashcards

Study Notes

Syllabus

  • R Programming is a course for III B.Sc CSDA students.
  • The course is for Semester V.
  • Syllabus includes 5 units covering R programming, data structures, operations, data frames, modeling, and classes & objects.

Unit I

  • Introducing R: Introduction to R, R data structures, functions in R, vectors, scalars, declarations, recycling, vectorized operations, NA and NULL values, filtering, and vector element names.
  • Features of R Programming: Open-source, strong graphical capabilities, highly active community, a wide selection of packages, comprehensive environment, complex statistical calculations, distributed computing, running code without a compiler, interfacing with databases, data variety, machine learning, and data wrangling. Cross-platform support and compatibility with other programming languages are also noted.

Unit II

  • Matrices and Operations: Creating matrices, matrix operations, applying functions to matrix rows and columns, adding and deleting rows/columns, vector/matrix distinction, avoiding dimension reduction, higher dimensional arrays, lists, creating lists, general list operations, accessing list components, values, applying functions to lists, and recursive lists.

Unit III

  • Data Frames: Creating data frames, matrix- operations in frames, merging data frames, applying functions to data frames, factors, tables, factors and levels, common functions used with factors, working with tables, other factors and table-related functions, control statements, arithmetic and Boolean operators, default values for arguments, returning Boolean values, Functions are objects, environment and scope issues, writing upstairs, recursion, replacement functions, tools for composing function code, math and simulation in R.

Unit IV

  • Classes and Objects: S3 and S4 classes, managing objects, input/output, accessing keyboard and monitor, reading and writing files, accessing the internet, string manipulation, graphics, creating graphs, customizing graphs, saving graphs, and creating three-dimensional plots.

Unit V

  • Interfacing R to other languages, Parallel R, basic statistics, linear model, generalized linear models, non-linear models, time series, and autocorrelation, clustering.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

R Programming Syllabus PDF

Description

Test your knowledge of fundamental R programming concepts, including functions like round(), operator usage, and vector handling. This quiz covers various aspects of R syntax and functionality, providing a comprehensive overview for beginners.

More Like This

C Programming Functions Quiz
5 questions
C Programming Functions Quiz
12 questions
C Programming Functions Quiz
5 questions
MATLAB: Functions, Vectors, and Equations
5 questions
Use Quizgecko on...
Browser
Browser