Installing R:
Installing RStudio:
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