In this topic in week 1, you will learn about :
Basic Features of R Programming
R is a powerful and popular programming language widely used for data analysis, statistical computing, and graphical representation. It has several essential features that make it a favorite choice among data scientists, statisticians, and researchers. Below are some of the key features of R programming:
R is an open-source language, which means it is freely available for anyone to use, modify, and distribute. The open-source nature encourages collaboration and continuous improvement of the language.
R provides efficient data handling capabilities, making it easy to import, manipulate, and transform data. It supports various data structures, such as vectors, matrices, data frames, and lists.
R is primarily known for its extensive statistical capabilities. It offers a vast collection of built-in functions and packages for conducting various statistical analyses, including regression, hypothesis testing, ANOVA, and time series analysis.
R excels in producing high-quality visualizations and graphs. It offers powerful plotting functions and packages like ggplot2, lattice, and base graphics, enabling users to create insightful and publication-quality plots.
R is an extensible language, allowing users to create their functions and packages to extend its functionality. The Comprehensive R Archive Network (CRAN) hosts thousands of user-contributed packages for various purposes.
R promotes reproducibility in data analysis and research. Scripts written in R can be easily shared and rerun on different datasets, ensuring that others can replicate and verify the results.
Documentation and Community Support: R has comprehensive documentation available through manuals, guides, and tutorials. Additionally, it has a vibrant and active community of users and developers who provide support through forums and mailing lists.
R offers an interactive environment with an interactive console and script editor, allowing users to execute commands interactively and see immediate results.
R can be integrated with other programming languages like Python, C++, and Java, enabling users to leverage their existing code and tools seamlessly.
Summary:
R programming offers a wide range of features for data analysis, statistics, visualization, and reproducibility. Its open-source nature, extensive statistical libraries, and active community support make it a preferred choice for data analysis and research in various domains. Whether you are a beginner or an experienced data analyst, R provides a powerful and flexible environment to explore, analyze, and visualize data effectively.
Installing and using R is a straightforward process. Here’s a step-by-step guide to help you get started:
Go to the R Project website: https://www.r-project.org/ Click on “CRAN” (Comprehensive R Archive Network) under “Download and Install R.” Select a CRAN mirror location near you.
Choose the appropriate operating system (Windows, macOS, or Linux). Download the R installer for your operating system.
For Windows: Double-click the downloaded installer file and follow the on-screen instructions to install R.
For macOS: Double-click the downloaded package file, and the installation process will begin.
For Windows: You can find the R program in the Start Menu or on your desktop. Double-click the R icon to launch the R console.
For macOS: You can find the R application in the Applications folder. Double-click the R icon to launch the R console.
After launching R, you will see the R console where you can type commands and interact with R. To execute a command, type it in the console and press Enter.
For example, try typing ‘print(“Hello, R!”)’ and press Enter. R will print the message “Hello, R!”.
R has a vast collection of packages contributed by the community. To install packages, you can use the ‘install.packages() function’.
For example, to install the ‘ggplot2’ package for data visualization, type ‘install.packages(“ggplot2”)’ and press Enter.
Once a package is installed, you need to load it into your R session before using its functions. To load a package, use the library( ) or require( ) function.
For example, to load the ggplot2 package, type library(ggplot2) and press Enter.
Writing code directly in the console is suitable for small tasks, but for more complex analyses or projects, it’s best to use R scripts.
Create a new plain text file with a .R extension, e.g., myscript.R. Write your R code in the script file using a text editor or an integrated development environment (IDE).
Save the file and then open it in the R console using the source( ) function. For example, source(“myscript.R”).
When you create plots in R, they will typically appear in a separate window or plot pane. You can interact with the plots using buttons or options in the plot pane (for GUI-based interfaces) or by typing additional commands to modify or save the plots.
Step 9: Exiting R
To exit R, type q( ) or quit( ) in the R console and press Enter. Congratulations! You have successfully installed and started using R. Now, you can explore R’s vast capabilities for data analysis, visualization, and statistical modeling to suit your needs.
Type of operators in R
1. ASSIGNMENT OPERATORS
The assignment operators in R allows you to assign data to a named object in order to store the data.
Example
## [1] 2
## [1] 6
## [1] 3
## [1] 7
## [1] 8
2. ARITHMETIC OPERATORS
These operators are used to carry out mathematical operations like addition and multiplication. Here is a list of arithmetic operators available in R.
Example
## [1] 8
## [1] 5
## [1] 35
## [1] 0.5
## [1] 256
## [1] 256
## [1] 2
## [1] 1
3. RELATIONAL/COMPARISON OPERATORS
Comparison or relational operators are designed to compare objects and the output of these comparisons are of type boolean. To clarify, the following table summarizes the R relational operators.
Example
## [1] TRUE
## [1] FALSE
## [1] TRUE
## [1] FALSE
## [1] FALSE
## [1] TRUE
4. LOGICAL/BOOLEAN OPERATORS
Example
## [1] 1 0 0 6
## [1] FALSE TRUE TRUE FALSE
Operators & and | perform element-wise operation producing result having length of the longer operand.
## [1] FALSE FALSE FALSE TRUE
## [1] TRUE TRUE FALSE TRUE
But && and || examines only the first element of the operands resulting into a single length logical vector.
x<-c(TRUE, FALSE, 0,6) y <- c(FALSE,TRUE,FALSE,TRUE)
x&&y
Zero is considered FALSE and non-zero numbers are taken as TRUE.
x<-c(TRUE, FALSE, 0,6) y <- c(FALSE,TRUE,FALSE,TRUE)
x||y
Let’s summarise the main points of this tutorial.
A work by Suriyati Ujang
suriyatiujang@uitm.edu.my