R Weekly Bulletin Vol – XIV

[This article was first published on R programming, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This week’s R bulletin covers some interesting ways to list functions, to list files and illustrates the use of double colon operator.

We will also cover functions like path.package, fill.na, and rank. Click To TweetHope you like this R weekly bulletin. Enjoy reading!


Shortcut Keys

1. New document – Ctrl+Shift+N
2. Close active document – Ctrl+W
3. Close all open documents – Ctrl+Shift+W

Problem Solving Ideas

How to list functions from an R package

We can view the functions from a particular R package by using the “jwutil”s package. Install the package and use the lsf function from the package. The syntax of the function is given as:


Where pkg is a character string containing package name.

The function returns a character vector of function names in the given package.



How to list files with a particular extension

To list files with a particular extension, one can use the pattern argument in the list.files function. For example to list CSV files use the following syntax:


# This will list all the csv files present in the current working directory.
# To list files in any other folder, you need to provide the folder path.

files = list.files(pattern = "\\.csv$")

# $ at the end means that this is end of the string.
# Adding \. ensures that you match only files with extension .csv

list.files(path = "C:/Users/MyFolder", pattern = "\\.csv$")

Using the double colon operator

The double colon operator is used to access exported variables in a namespace. The syntax is given as:


Where pkg is the package name symbol or literal character string. The name argument is the variable name symbol or literal character string.

The expression pkg::name returns the value of the exported variable from the package if it has a namespace. The package will be loaded if it was not loaded already before the call. Using the double colon operator has its advantage when we have functions of the same name but from different packages. In such a case, the sequence in which the libraries are loaded is important.

To see the help documentation for these colon operators you can run the following command in R – ?’::’ or help(“:::”)



first = c(1:6)
second = c(3:9)

dplyr::intersect(first, second)
[1] 3 4 5 6
base::intersect(first, second)
[1] 3 4 5 6

In this example, we have two functions having the same names but from different R packages. In some cases, functions having same names can produce different results. By specifying the respective package name using the double colon operator, R knows in which package to look for the function.

Functions Demystified

path.package function

The path.package function returns path to the locations where the given package is found. If the package is not mentioned then the function will return the path of the all the currently attached packages. The syntax of the function is given as:

path.package(package, quiet = FALSE)

The quiet argument takes a default value of False. If this is changed to True then it will throw a warning if the package named in the argument is not attached and will give an error if none are attached.



fill.na function

There are different R packages which have functions to fill NA values. The fill.na function is part of the mefa package and it replaces NA values with the nearest values above them in the same column.The syntax of the function is given as:


Where, x can be a vector, a matrix or a data frame.


x = c(12,NA,15,17,21,NA)

rank function

The rank function returns the sample ranks of the values in a vector. Ties (i.e., equal values) and missing values can be handled in several ways.

rank(x, na.last = TRUE, ties.method = c(“average”, “first”, “random”, “max”,”min”))

x: numeric, complex, character or logical vector
na.last: for controlling the treatment of NAs. If TRUE, missing values in the data are put last; if FALSE, they are put first; if NA, they are removed; if “keep” they are kept with rank NA
ties.method: a character string specifying how ties are treated


x = c(3, 5, 1, -4, NA, Inf, 90, 43)

rank(x, na.last = FALSE)

Next Step

We hope you liked this bulletin. In the next weekly bulletin, we will list more interesting ways and methods plus R functions for our readers.

The post R Weekly Bulletin Vol – XIV appeared first on .

To leave a comment for the author, please follow the link and comment on their blog: R programming.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)