Functions in R Language: Quick Guide 1

Functions in R language (or any programming language) are fundamental building blocks that allow you to organize the programming code, make it reusable, and perform complex tasks efficiently.

Functions in the R Language are first-class objects of the class function and can be passed by arguments to other functions. Functions can be assigned to variables, stored in a list, passed as arguments to other functions, created functions inside functions, and even returned function as the result of a function. There are three building blocks of functional programming: anonymous functions, closures (functions written by functions), and a list of functions.

Components of a Function

Each function in the R Language consists of three components

  1. formal( )
  2. body( )
  3. environment( )

Types of Function

There are two main types of functions in R:

  1. Built-in functions in R: There is a vast library of built-in functions in R Language, like finding the mean (mean()), calculating the sum (sum()), or creating graphs (plot()).
  2. User-defined functions in R: One can create functions to tailor them to one’s needs. This is useful for repetitive tasks, improving code readability, and avoiding errors.

Each function has arguments that can be given default values, which makes interactive usage more convenient. A function is defined by an assignment of the form

name <- function(arg1, arg2, …){
     Expression
}

where the expression uses the arg1, arg2, ... (arguments) to calculate a value. The value of the expression is the value returned for the function. A call to function usually takes the form

name(expr1, expr2, …)

and may occur anywhere a function call is legitimate.

Functions in R Language: Example

As an example, consider the following customized function (user-defined function) center(). This function can compute the mean, median, and trimmed mean of the input data. The center() function has two arguments, the first argument is for data and the second argument is for the selection of summary statistics.

center<-function(x, type){
    type == "mean" && return(mean(x))
    type == "median" && return(median(x))
    type == "trimmed" && return(mean(x, trim=0.1))
}

As another example, the user-defined summary( ) function is created without repetition of some arguments, i.e. duplication is removed. Note that all the functions in the user-defined function are stored as a list.

summary <- function(x) {
     funs <- c(mean, median, sd, mad, IQR)
     lapply(funs, function(f) f(x, na.rm = TRUE))
}
Functions in R Language

The center() function is created to perform some summary statistics using the switch() statement.

center<-function(x, type){
    switch(type,
         mean = mean(x),
         median = median(x),
         trimmed = mean(x,trim=0.1)
    )
}

Let us generate the data from normal distribution and check the output from the user-defined function center().

x <- rnorm(100)

center(x, type="mean")
center(x, type="median")
center(x, type="trimmed")
center(x, type="mode")
Functions in R Language

FAQs about Functions in R Language

  1. What is a function in R?
  2. Describe the components of a function
  3. Give some working examples of customized functions in R.
  4. What is meant by arguments of a function
  5. Differentiate between built-in and user-defined functions in R Language.

https://itfeature.com

https://gmstat.com

Important R Language Questions

The post is about R Language Questions that are commonly asked in interviews or R Language-related examinations and tests.

R Language Questions

Question: What is a file in R?
Answer: A script file written in R has a file extension of R. Since, R is a programming language designed to perform statistical computing and graphics on given data, that is why, a file in R contains code that can be executed within the R software environment.

Question: What is the table in R?
Answer: A table in R language is an arbitrary R object, that is inherited from the class “table” for the as.data.frame method. A table in R language refers to a data structure that is used to represent categorical data and frequency counts. A table provides a convenient way to summarize and organize the data into a tabular format, making it easier to analyze and interpret.

Factor Variables in R

Questions: What is the factor variable in R language?
Answer: Factor variables are categorical variables that hold either string or numeric values. The factor variables are used in various types of graphics, particularly for statistical modeling where the correct number of degrees of freedom is assigned to them.

Data Structure in R

Questions: What is Data Structure in R?
Answer: A data structure is a specialized format for organizing and storing data. General data structure types include the array, the file, the record, the table, the tree, and so on. R offers several data structures, each with its characteristics and purposes. In R common data structures are: vector, factor, matrix, array, data frame, and lists.

scan() Function in R

Question: What is a scan() in R?
Answer: The scan() in R is used to Read Data Values: Read data into a vector or list from the console or file. For Example:

Z <- scan()
1: 12 5
3: 2
4:
Read 3 items

> z
[1] 12 5 2
R Language Questions FAQS Logo

readline() Function in R

Questions: What is readline() in R?
Answer: The deadline() function in R, read text lines from a Connection: Read some or all text lines from a connection. One can use readline() for inputting a line from the keyboard in the form of a string. For Example:

w <- readline()
xyz vw u
> w

[1] "xyz vw u"

R and Data Analysis

MCQs in Statistics

Computer MCQs Online Test

Important MCQs R Markdown Quiz 12

Online MCQs R Markdown Quiz with Answers. R Markdown is a way of generating fully reproducible documents, in which both text and code can be combined. Let us start with the MCQs R Markdown Quiz.

Online MCQs about R Markdown with Answers

1. R Markdown notebooks can be converted into HTML, PDF, and Word documents, slide presentations, and _________.

 
 
 
 

2. A data analyst wants to convert their R Markdown file into another format. What are their options?

 
 
 
 

3. If an analyst creates the same kind of document over and over or customizes the appearance of a final report, they can use __________ to save them time.

 
 
 
 

4. A data analyst creates an interactive version of their R Markdown document to share with other users allowing them to execute code the analyst wrote. What did they create?

 
 
 
 

5. A data analyst wants to create documentation for their cleaning process so other analysts on their team can recreate this process. What tool can help them create this shareable report?

 
 
 
 

6. What information does a data analyst usually find in the header section of an RMarkdown document?

 
 
 
 

7. A data analyst needs to create a shareable report in RStudio. They first want to change the default file format that gets exported by the Knit button to .pdf. What value should they use for the output field in the YAML header?

 
 
 
 

8. A data analyst has finished editing their R Markdown file and wants to save it as an HTML report. What tool will they use?

 
 
 
 

9. A data analyst wants to embed a link in their RMarkdown document. They write (click here!)(www.rstudio.com) but it doesn’t work. What should they write instead?

 
 
 
 

10. A data analyst wants to find headers in their R Markdown document. What should they look for?

 
 
 
 

11. What is the purpose of the Knit button in R Studio?

 
 
 
 

12. To create bullet points in their output document, a data analyst adds __________ to their RMarkdown document.

 
 
 
 

13. Fill in the blank: Markdown is a _________ for formatting plain text files.

 
 
 
 

14. A data analyst notices that their header is much smaller than they wanted it to be. What happened?

 
 
 
 

15. A delimiter is a character that indicates the beginning or end of __________.

 
 
 
 

16. A data analyst has code chunks in their R Markdown file. How do they appear in an HTML report?

 
 
 
 

MCQs R Markdown Quiz

MCQs R Markdown Quiz

  • A data analyst wants to find headers in their R Markdown document. What should they look for?
  • A data analyst has code chunks in their R Markdown file. How do they appear in an HTML report?
  • Fill in the blank: Markdown is a ———- for formatting plain text files.
  • A data analyst creates an interactive version of their R Markdown document to share with other users allowing them to execute code the analyst wrote. What did they create?
  • A data analyst wants to convert their R Markdown file into another format. What are their options?
  • A data analyst has finished editing their R Markdown file and wants to save it as an HTML report. What tool will they use?
  • What information does a data analyst usually find in the header section of an RMarkdown document?
  • To create bullet points in their output document, a data analyst adds ———- to their RMarkdown document.
  • A data analyst wants to embed a link in their RMarkdown document. They write (click here!)(www.rstudio.com) but it doesn’t work. What should they write instead?
  • A data analyst needs to create a shareable report in RStudio. They first want to change the default file format that gets exported by the Knit button to .pdf. What value should they use for the output field in the YAML header?
  • What is the purpose of the Knit button in R Studio?
  • A delimiter is a character that indicates the beginning or end of ———-.
  • R Markdown notebooks can be converted into HTML, PDF, and Word documents, slide presentations, and ———-.
  • A data analyst notices that their header is much smaller than they wanted it to be. What happened?
  • A data analyst wants to create documentation for their cleaning process so other analysts on their team can recreate this process. What tool can help them create this shareable report?
  • If an analyst creates the same kind of document over and over or customizes the appearance of a final report, they can use ———- to save them time.

MCQs in Statistics, R Data Analysis