Introduction to R :
R is a powerful, open-source programming language and environment designed for statistical computing and data analysis. Widely used by data scientists and statisticians, R offers extensive libraries, data visualization tools, and statistical techniques. Its flexibility and versatility make it a preferred choice for exploring, modeling, and visualizing data.
Basic components Variables:
Store and manipulate data. Functions: Perform operations or computations. Data Structures: Organize and store data. Control Structures: Control the flow of program execution (e.g., loops and conditionals). Libraries (Packages): Extend R’s capabilities with specialized functions. Data Import/Export: Read and write data from/to various formats. Documentation: Add comments and documentation for code clarity. Graphics: Create visualizations and plots.
Datatypes in R Numeric:
Represents numbers (e.g., 3.14, -42). Character (String): Stores text (e.g., “Hello, World!”). Logical: Represents TRUE or FALSE values. Date and Time: Handles date and time values.
# Define variables with different data types
a <- 42
string <- "Hello, World!"
logic <- TRUE
date_time <- as.POSIXct("2004-10-04 14:30:00")
# Print variables
cat("Numeric Variable:",a, "\n")
## Numeric Variable: 42
print("The character variable is:")
## [1] "The character variable is:"
print(string)
## [1] "Hello, World!"
print("The logical variable is:")
## [1] "The logical variable is:"
print(logic)
## [1] TRUE
cat("Date and Time Variable:", date_time, "\n")
## Date and Time Variable: 1096880400
# Create data structures
vector1<- c(1, 2, 3, 4, 50,43,32)
matrix1<- matrix(1:9, nrow = 3, ncol = 3)
list1 <- list(04, "john", FALSE)
data_frame<- data.frame(
Name = c("Kruthi", "teju", "Abhi","Vishnu","Prachi"),
Age = c(20,20,23,45,44),
Score = c(91, 81, 55,100,11)
)
# Print data structures
print(vector1)
## [1] 1 2 3 4 50 43 32
print("Matrix Example")
## [1] "Matrix Example"
print(matrix1)
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
print("List Example")
## [1] "List Example"
print(list1)
## [[1]]
## [1] 4
##
## [[2]]
## [1] "john"
##
## [[3]]
## [1] FALSE
print("The dataframe is:")
## [1] "The dataframe is:"
print(data_frame)
## Name Age Score
## 1 Kruthi 20 91
## 2 teju 20 81
## 3 Abhi 23 55
## 4 Vishnu 45 100
## 5 Prachi 44 11
# b.Create a vector
vector5 <- c(2, 4, 6, 8, 10)
sum_result <- sum(vector5)
mean_result <- mean(vector5)
product_result <- prod(vector5)
# Print the results
print(paste("Sum:", sum_result))
## [1] "Sum: 30"
print(paste("Product:", product_result))
## [1] "Product: 3840"
# c.Create a vector
my_vector <- c(3, 1, 7, 2, 9)
# Find the minimum and maximum
min_value <- min(my_vector)
max_value <- max(my_vector)
# Print the results
print(paste("Minimum:", min_value))
## [1] "Minimum: 1"
print(paste("Maximum:", max_value))
## [1] "Maximum: 9"
#d. Create a list
my_list <- list(
string_element = "Hello, World",
numeric_element = 42,
vector_element = c(1, 2, 3),
logical_element = TRUE
)
# Print the list
print(my_list)
## $string_element
## [1] "Hello, World"
##
## $numeric_element
## [1] 42
##
## $vector_element
## [1] 1 2 3
##
## $logical_element
## [1] TRUE
#e. Create a list with named elements
my_list <- list(
vector_element = c(1, 2, 3),
matrix_element = matrix(1:6, nrow = 2),
nested_list = list(a = "apple", b = "banana")
)
# Access the first and second elements of the list
first_element <- my_list$vector_element
second_element <- my_list$matrix_element
# Print the accessed elements
print(first_element)
## [1] 1 2 3
print(second_element)
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
#f. Create a 3x5 matrix filled with zeros
my_matrix <- matrix(0, nrow = 3, ncol = 5)
# Print the matrix
print(my_matrix)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0 0 0 0 0
## [2,] 0 0 0 0 0
## [3,] 0 0 0 0 0
#g. Create a sample matrix
my_matrix <- matrix(1:12, nrow = 3)
# Access specific elements
element_1 <- my_matrix[2, 3] # 3rd column, 2nd row
element_2 <- my_matrix[3, ] # 3rd row
element_3 <- my_matrix[, 4] # 4th column
# Print the accessed elements
print(element_1)
## [1] 8
print(element_2)
## [1] 3 6 9 12
#h. Create vectors
name <- c("Alice", "Bob", "Charlie")
age <- c(25, 30, 35)
# Create a DataFrame
df <- data.frame(Name = name, Age = age)
# Display the DataFrame
print(df)
## Name Age
## 1 Alice 25
## 2 Bob 30
## 3 Charlie 35
#i. Create a DataFrame
df <- data.frame(Name = c("Alice", "Bob"), Age = c(25, 30))
# New data to insert
new_data <- data.frame(Name = c("Charlie", "David"), Age = c(35, 40))
# Insert new rows
df <- rbind(df, new_data)
# Display the updated DataFrame
print(df)
## Name Age
## 1 Alice 25
## 2 Bob 30
## 3 Charlie 35
## 4 David 40
#j. Create a DataFrame
df <- data.frame(Name = c("Alice", "Bob"), Age = c(25, 30))
# Add a new column
df$Salary <- c(50000, 60000)
# Display the updated DataFrame
print(df)
## Name Age Salary
## 1 Alice 25 50000
## 2 Bob 30 60000
#k. Create a DataFrame
df <- data.frame(Name = c("Alice", "Bob", "Charlie", "David"), Age = c(25, 30, 35, 40))
# Extract the first 2 rows
first_two_rows <- df[1:2, ]
# Display the extracted rows
print(first_two_rows)
## Name Age
## 1 Alice 25
## 2 Bob 30