Skip to content

R Frequently Asked Questions

Statistical Computing and Graphics in R

Menu
  • Learn R
    • R Basics
      • R FAQS about Package
      • R GUI
      • Using R packages
      • Missing Values
    • R Graphics
    • Data Structure
      • Data Frame
      • Matrices
      • List
    • R Programming
    • Statistical Models
  • R Quiz
    • MCQs R Programming
    • R Basic Quiz 7
    • MCQs R Debugging 6
    • MCQs R Vectors 5
    • R History & Basics 4
    • R Language Test 3
    • R Language MCQs 2
    • R Language MCQs 1
  • MCQs
    • MCQs Statistics
      • MCQs Basic Statistics
      • MCQs Probability
      • MCQs Graph & Charts
      • MCQs Sampling
      • MCQs Inference
      • MCQs Correlation & Regression
      • MCQs Time Series
      • MCQs Index Numbers
      • MCQs Quality Control 1
    • MCQS Computer
    • MCQs Mathematics Part-I
  • About ME
  • Contact Us
  • Glossary

Category: Advance R Programming

Introduction to R Packages

No Comments
| Advance R Programming

In R language functions and datasets are all stored in packages. The content of a package is only available when a package is loaded using the library() function.

To see which R packages are installed, write the following command (without argument)

> library()

To load a particular installed package, use package name as the argument to the library() function, that is,

> library(MASS)

If the computer system is connected to the internet and a required package is not installed on one’s computer, the user can use the install.packages() function to install the required package. To update the already installed package one can use the update.package() function. The search() function can be used to see which packages are loaded into computer memory.

R packages can be classified as standard (base) packages and contributed packages. The standard (or base) packages are considered part of the R source code. The base packages contain the basic functions that allow R to work. The base packages also contain datasets, standard statistical and graphical functions. The standard R functions are automatically available in any R installation, that is, you don’t need to install them.

The standard R packages are written by authors. These packages implement some specialized statistical methods, access to datasets and hardware. The contributed packages are distributed with every binary distribution of R and are available for download from CRAN and other repositories such as BioConductor.

R packages can have a namespace. Namespaces (i) allows the package writer to hide functions and data that are meant only for internal use, (ii) prevent functions from breaking when a user picks a name that clashes with one in the packages, and (iii) they provide a way to refer to an object within a particular package.

For example, in R the t() function is the transpose function. A user can define his own t() function. The namespaces will prevent the user’s definition from taking procedure and breaking every function that tries to transpose matrix.

There are two operators that work with namespaces, (i) :: double colon operator and triple colon operator :::. The double colon operator selects definitions from a particular namespace. For example, t() function is available as the base::t, because it is defined in the base package. The function that is exported from the package can be retrieved with a double colon operator.

The tiple colon operator acts as a double colon operator but it also allows access to hidden objects. The getAnywhere() function can be used to search for multiple packages.

Note: Packages are inter-dependent, and loading one package may cause other packages to be automatically loaded. The colon operators also cause automatic loading of the associated package. the package is not added to the search list when a package with namespaces is loaded automatically.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Skype
  • Tumblr
  • Pinterest
  • Print
  • WhatsApp
  • Telegram
  • Reddit
  • Pocket

Like this:

Like Loading...

Read More »

Debugging Tools in R Language

No Comments
| Advance R Programming

The built-in debugging tools (programs: functions) in R statistical computing environment are traceback(), debu(), browser(), trace(), and recover(). The purpose of the debugging tools is to help the programmer find unforeseen (غیر متوقع، جس کی اُمید نہ ہو) problems quickly and efficiently.

The R system has two main ways of reporting a problem in executing a function. One of them is a warning message while the other one is a simple error. The purpose of the warning is to tell the user (programmer) that “something unusual happened during the execution of the function, but the function was nevertheless able to execute to completion”. Writing a robust code (code which checks for imputing errors) is important for larger programs.

> log(-1)     #produce a warning (NaN)
> message <- function(x){
            if(x > 0)
            print(“Hello”)
            else
            print(“Goodbye”)
   }

The log(-1) will result in a fatal error, not a warning. The first thing one should do is to print the call stack (print the sequence of function calls which led to the error). The traceback() function can be used which prints the list of functions which were called before the error occurred. However, this can be uninteresting if the error occurred at a top-level function.

The traceback() Function

The traceback() function prints the sequence of function calls in reverse order from the top.

The debug() Function

The debug() function takes a single argument (the name of a function) and steps through the function line-by-line to identify the specific location of a bug, that function is flagged for debugging. In order to unflag a function, undebug() function is used. A function flagged for debugging does not execute in a usual way, rather, each statement in the function is executed one at a time and the user can control when each statement gets executed. After a statement is executed, the function suspends and the user is free to interact with the environment.

The browser() Function

The browser() function can be used to suspend execution of a function so that the user can browse the local environment.

The trace() Function

The trace() function is very useful for making minor modifications to function “on the fly” without having to modify functions and re-sourcing them. If is especially useful if you need to track down an error which occurs in a base function.

> trace(“mean”, quote( if( any(is.nan(x) ) ){ browser() }), print = FALSE)

The trace() function copy the original function code into a temporary location and replaces the original function with a new function containing the insert code.

The recover() Function

The recover() function can help to “jump up” to a higher level in the function call stack.

> options(error=recover)

The error option tells R what to do in the situation where a function must halt the execution.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Skype
  • Tumblr
  • Pinterest
  • Print
  • WhatsApp
  • Telegram
  • Reddit
  • Pocket

Like this:

Like Loading...

Read More »

Namespaces in R Language

No Comments
| Advance R Programming, R FAQS about Package, R Programming

In R language, the packages can have namespaces, and currently, all of the base and recommended packages do except the dataset packages. Understanding the use of namespaces is vital if one’s plan to submit a package to CRAN because CRAN requires that the package plays nicely with other submitted packages on CRAN. Namespaces ensure that other packages will not interfere with your code and the package works regardless of the environment in which it’s run.

For example, plyr and Hmisc both provide a function namely summarize(). Loading plyr package and then Hmise, the summarize() function will refer to the Hmisc. However, loading the package in the opposite order, the summarize() function will refer to the plyr package version.

To avoid confusion, one can explicitly refer to the specific function, for example,

> Hmisc::summarize()

and

> plyr::summarize()

Now, the order in which the packages are loaded would not matter.

Namespaces do three things:

  • Namespaces allow the package writer to hide functions and data that are meant only for internal use,
  • Namespaces prevent functions from breaking when a user (or other package writers) picks a name that clashes with one in the package, and
  • Namespaces provide a way to refer to an object within a particular package

Namespace Operators

In R language, there are two operators that work with namespaces.

  • Doule-Colon Operator
    The double-colon operator:: selects definitions from a particular namespace. The transpose function t() will always be available as the base::t, because it is defined in the base package. Only functions that are exported from the package can be retrieved in this way.
  • Triple-Colon Operator
    The triple-colon operator ::: acts like the double-colon operator but also allows access to hidden objects. Users are more likely to use the getAnywhere() function, which searches multiple packages.

Packages are often inter-dependent, and loading one may cause others to be automatically loaded. The colon operators will also cause automatic loading of the associated package. When packages with namespaces are loaded automatically they are not added to the search list.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Skype
  • Tumblr
  • Pinterest
  • Print
  • WhatsApp
  • Telegram
  • Reddit
  • Pocket

Like this:

Like Loading...

Read More »

Subscribe via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 265 other subscribers

Search Form

Facebook

Facebook

Categories

  • Advance R Programming (3)
  • Data Analysis (12)
    • Comparisons Tests (2)
    • Statistical Models (10)
  • Data Structure (9)
    • Data Frame (2)
    • Factors in R (1)
    • List (2)
    • Matrices (2)
    • Vectors in R (1)
  • Importing/ Exporting Data (4)
    • R Data Library (4)
  • R Control Structure (3)
    • For loop in R (1)
    • Switch Statement (1)
  • R FAQS (18)
    • Missing Values (2)
    • R Basics (12)
    • R FAQS about Package (3)
    • R Programming (2)
  • R Graphics (4)
    • Exploring Data in R (1)
    • plot Function (2)
  • R Language Basics (4)
  • R Language Quiz (8)
  • Using R packages (2)
https://www.youtube.com/watch?v=MZpiMyAfnYQ&list=PLB01qg3XnNiMbKkvP2wYzzHkv6ZekaKZx

Posts: itfeature.com: Basic Statistics and Data Analysis

MCQs Chi-Square Association 2

The relationship/ Dependency (also called Association) between the attributes is called relationship/association and the measure of degrees of relationship between the attributes is called the coefficient of association. The Chi-Square Statistic is used to…

Short Questions Sampling and Sampling Distributions 1

The post is about some important Short Questions about sampling and sampling distribution. Q1: Define Sample and Sampling. Answer: Sample: A small portion of the population representing the qualities of the population being sampled…

MCQs IBM SPSS-1

Online MCQs about IBM SPSS with answers.

MCQs Correlation and Regression 6

This Quiz contains MCQs about Correlation and Regression Analysis, Multiple Regression Analysis, Coefficient of Determination (Explained Variation), Unexplained Variation, Model Selection Criteria, Model Assumptions, Interpretation of results, Intercept, Slope, Partial Correlation, Significance tests, OLS Assumptions,…

Short Questions: Normal and Standard Normal Distribution

The following post is about Short Questions related to Normal and Standard Normal Distribution. Q1: What is a standard normal variable? Ans: The variable $Z=\frac{X-\mu}{\sigma}$ which measures the deviations of variable $X$ from the…

Posts: gmstat.com: GM Statistics

MCQs Number System – 4

MCQs Economics – 3

MCQs Economics – 2

Try MCQs Economics Test 1

MCQs Economics – 1

MCQs Econometrics Quiz 5

This quiz is about Econometrics, which covers the topics of Regression analysis, correlation, dummy variable, multicollinearity, heteroscedasticity, autocorrelation, and many other topics. Let’s start with MCQs Econometrics test An application of different statistical methods applied to the economic data used…

R Frequently Asked Questions 2023 . Powered by WordPress

%d bloggers like this:
    pixel