Skip to Main Content

Data Visualization with R

Getting started with R

Installing R:

  1. Download R: Go to the official R website (https://cran.r-project.org/) and select a CRAN mirror (a location from which R can be downloaded). Choose a mirror that's geographically close to you.
  2. Choose Your OS: Select the appropriate version of R for your operating system (Windows, macOS, or Linux).
  3. Install R: Follow the installation instructions provided for your OS. This usually involves running the installer and following the prompts.

Installing RStudio:

  1. Download RStudio: Go to the RStudio website (https://www.rstudio.com/) and navigate to the "RStudio" section.
  2. RStudio Desktop: Download the free version of RStudio Desktop. There's also a paid version called RStudio Desktop Pro with additional features.
  3. Install RStudio: Run the RStudio installer and follow the installation instructions. RStudio typically detects your R installation and integrates with it.

Basic R Syntax

R is the most popular language used for Statistical Computing and Data Analysis with the support of over 10, 000+ free packages in CRAN repository. Like any other programming language, R has a specific syntax which is important to understand if you want to make use of its powerful features.

1. Assigning Values: In R, variables are assigned using the <- operator (preferred) or =. For example:

x <- 10 
name <- "John" 
is_true <- TRUE

2. Data Types: R supports various data types, including numeric, character, logical, and more:

number <- 42 
text <- "Hello, R!" 
logical_value <- FALSE

3. Vectors: Vectors are fundamental to R. Create them using the c() function:

numbers <- c(1, 2, 3, 4, 5) 
fruits <- c("apple", "banana", "orange")

4. Basic Operations: Perform arithmetic operations and more complex calculations:

result <- 3 + 5 
square <- 4^2

5. Functions: R offers a rich collection of functions. Call them with arguments:

sqrt_value <- sqrt(25) 
len <- length(fruits)

6. Data Frames: Data frames are tabular structures for managing data with rows and columns:

data <- data.frame(names = c("Alice", "Bob", "Carol"), 
                     ages = c(25, 30, 28))

7. Indexing: Access elements using indexing (1-based):

first_num <- numbers[1] 
second_fruit <- fruits[2]

8. Conditional Statements: Use if, else if, and else for conditions:

if (x > 0) 
{    print("Positive") 
}  else if (x == 0) {   
     print("Zero") } 
   else { 
     print("Negative") }

9. Loops: Utilize for and while loops for iteration:

for (i in 1:5) { 
     print(i) 
}

As you grasp these foundational concepts of R syntax, you'll be well-equipped to dive into more advanced topics like data manipulation, visualization, and statistical analysis. R's versatility and extensive libraries make it a powerful tool for data professionals across various domains