Top 20 most useful functions in R
You begin in R programming? That's a great idea! But you don't find the perfect function for your basic need? OK, here is a little guide to help you to find your way among the most used and most useful function for your scripts.
Manipulate files / folders in R
Think you that you need to use the explorer of your computer to explore your files and folders in R?
No, R have several functions included to access the elements of your computer. Let's explore some of the most useful functions.
- ls()
List of the elements of the programming environment - rm(x)
Delete the object x from your programming environment - dir()
List the elements of the work folder.
If the folder path is mentioned as a parameter, the function will return the elements of this directory. - files()
List the files of the folder. If the parameter isn't completed, the function will return the elements of the current directory. - getwd()
Return the path of the work directory. - setwd()
Set the path of the work directory.
Character string processing
Without a doubt the most used processing after arithmetic processing, there is some essential functions included in R.
- paste(x, y, z, ...)
Concatenate the elements mentioned as parameters and return a character string. - substr(x, start, stop)
Extract a character string from the x string and take into account the start and the end of the extraction. - strsplit(x, split)
Split a character string in terms of the seperator on the split parameter.
Information about objects
Adapt your scrips to works in a specific case is great, but if you want to keep your script useable in many cases, you have to test the values of your objects. Here is some basic function to help you.
- length(x)
Return the number of elements that contain the object - nrow(x)
Return the number of lines of the object. - ncol(x)
Return the number column the object - class(x)
Return the class of the object (list, data frame, integer...) - na(x), is.null(x)...
Test the value of an object and return TRUE is the object is equal to NA or if the object is NULL - exists(x)
Test if an object exists. If the object exists, the function returns TRUE, if not, FALSE.
Useful functions in R
At last, an overview of the query to help you to better understand the R functions and the environment.
- start()
Open the help summary - help(x) / ?
Open the help, replace x by the name of a function. For example, help(print) - ls("package")
Return the list of all the functions of a package - print(x)
Display the mentioned parameter on the terminal
Feel free to test these functions and use them on you R scripts 😉