# **Implementing and Styling Data with R – A Step-by-Step Approach**
# In this project, I explored the process of working with data in R, specifically using the readxl package for data handling and the kableExtra package for enhanced table formatting. By carefully going through each step, I gained a deeper understanding of loading datasets, creating variables, performing basic operations, and displaying data in a visually appealing format.
# 
# Installing and Loading Packages
# To start, I needed to install and load necessary packages in R, namely readxl for reading Excel files and kableExtra for table styling. I used the following command to install the readxl package, checking first to avoid redundant installations:
# Since I already installed the  'readxl' package I will comment it out and just add in the library. 
# install.packages("readxl")
library(readxl)
# Now I will install the kableExtra package and load the library so that way I will be able to format the tables better.
# install.packages("kableExtra")
library(kableExtra)
# **Loading and Viewing the Dataset**
# Now I have the required packages, I proceeded to load my dataset, and  stored it in an Excel file called  "R_Variables_Data_Structures.xlsx". This way i can specify the file path and utilize the read_excel function from the readxl package to load my data.
# **Loading in my dataset** 
r_concepts_data <- read_excel("C:/Users/jacob/Downloads/R_Variables_Data_Structures.xlsx")
# Next I will view the data in a separate window in a excel file format to inspect the data in a tabular format provided by RStudio. This way it will allow myself to see and understand the structure of the data.
View(r_concepts_data)
# **Creating and Manipulating Variables in R**
# To deepen my understanding of R’s data structures, I created a variety of variables representing different data types and performed some basic operations. I started with scalar variables, which are single values assigned using the <- operator:

# **Scalar variables**
a <- 1
b <- 2
# Next I will create vectors that will be represented as sequences of values for both numeric and character vectors.

# **Vector variables**
c <- c(2, 3, 4)
d <- c(10, 10, 10)
e <- c(1, 2, 3, 4)
# Next is sequence variable called "f" to be able to further the range of the data stucture. 

# **Sequence**
f <- 1:6
# Next, For matrices, I experimented with combining columns and rows to form 2D structures. This allowed me to better understand how R handles multi-dimensional data:

# **Matrix examples**
W <- cbind(1:4, 5:8, 9:12)
Z <- rbind(rep(0, 3), 1:3, rep(10, 3), c(4, 7, 1))
# Lastly, I included character variables, such as foo and .foo, with .foo being a hidden variable that doesn’t appear in the environment listing unless specified.

# Character variables
foo <- 'foo'
.foo <- 'bar'  # Hidden variable
# **Performing Operations on Variables**

# Now I have these variables created, I will perform some basic operations to test R Studio mathematical operations and vector recycling. For example, I added scalar values, performed vector addition and multiplication:

print(a + b)         # Scalar addition
## [1] 3
print(c + d)         # Vector addition
## [1] 12 13 14
print(c * d)         # Vector multiplication
## [1] 20 30 40
print(c + e)         # Vector addition with recycling
## Warning in c + e: longer object length is not a multiple of shorter object
## length
## [1] 3 5 7 6
print(exp(f))        # Exponential of sequence
## [1]   2.718282   7.389056  20.085537  54.598150 148.413159 403.428793
# Next I will be working with matrices by adding scalars to matrices and performing component-wise multiplication:

# Matrix operations
print(W + 1)         # Adding scalar to matrix
##      [,1] [,2] [,3]
## [1,]    2    6   10
## [2,]    3    7   11
## [3,]    4    8   12
## [4,]    5    9   13
print(Z * W)         # Component-wise multiplication of matrices
##      [,1] [,2] [,3]
## [1,]    0    0    0
## [2,]    2   12   30
## [3,]   30   70  110
## [4,]   16   56   12
print(foo)           # Printing character variable
## [1] "foo"
print(.foo)          # Printing hidden variable
## [1] "bar"
# Additionally, I used the ls(all.names = TRUE) function to list all variables, including hidden ones, reinforcing my understanding of how R treats different variable types in the environment.
# 
# Styling the Dataset Table with kableExtra
# To present the dataset in a polished and visually appealing way, I used the kableExtra package to style my table. By converting the dataset into an HTML table, I was able to add features such as striped rows, hover effects, condensed formatting, and custom colors:

# Display the table with styling
r_concepts_data %>%
  kable("html", caption = "R Variables and Data Structures") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), 
                full_width = FALSE, 
                position = "center") %>%
  row_spec(0, bold = TRUE, color = "white", background = "#4CAF50") %>%  # Header styling
  column_spec(1, bold = TRUE, color = "blue") %>%                         # Variable column styling
  column_spec(2, color = "darkgreen") %>%                                 # Type column styling
  column_spec(3, width = "20em") %>%                                      # Definition column styling and width
  column_spec(4, color = "darkred") %>%                                   # Example column styling
  column_spec(5, color = "purple") %>%                                    # Operation column styling
  column_spec(6, color = "darkorange")                                    # Result column styling
R Variables and Data Structures
Variable Type Definition Example Operation Result
a scalar a single value assigned as a scalar a <- 1 a + b 3
b scalar another single value scalar b <- 2 a * b 2
c vector vector of numeric values c <- c(2,3,4) c + d [1] 12 13 14
d vector vector of numeric values d <- c(10,10,10) c * d [1] 20 30 40
e vector vector of numeric values e <- c(1,2,3,4) c + e [1] 3 5 7 6 (warning due to recycling)
f sequence sequence of values from 1 to 6 f <- 1:6 exp(f) [1] 2.718, 7.389, 20.085, 54.598, …
W matrix 3-column matrix with values 1:4, 5:8, 9:12 W <- cbind(1:4,5:8,9:12) W + 1 Adds 1 to each matrix element in W
Z matrix 4-row matrix with different row values Z <- rbind(rep(0,3),1:3,rep(10,3),c(4,7,1)) Z * W Componentwise multiplication of Z and W
foo character character string for variable foo foo <- ‘foo’ foo ‘foo’
.foo character hidden/private character variable starting with dot .foo <- ‘bar’ .foo ‘bar’ (hidden variable)
# **Conclusion**

# Through this project, I gained valuable insights into data handling, variable creation, and table styling in R, and I had the opportunity to interpret some of the results generated from my data manipulations. By working with packages like readxl and kableExtra, I learned not only how to load and manipulate data but also how to present it in an accessible and aesthetically pleasing format, enhancing the readability and professionalism of my work. The results of this project underscore the versatility and power of R for data science tasks, and I have seen firsthand how these tools can streamline complex data analysis processes.
# 
# One key observation from my results was the effectiveness of using R’s data structures, such as vectors, matrices, and lists, to handle different types of data. For instance, I created scalar and vector variables to explore basic mathematical operations and noticed how R automatically performed element-wise calculations. This feature, while intuitive, also highlighted the importance of understanding R’s recycling rule when performing operations on vectors of unequal lengths. For example, when I added vectors of different lengths, R reused the shorter vector’s elements to complete the operation. This behavior is powerful but requires careful handling to avoid unintended results, as indicated by the warning I received during the addition of unequal-length vectors.
# 
# When working with matrices, I experimented with adding scalars and performing component-wise multiplication, which revealed R’s flexibility with matrix operations. The results from these operations demonstrated R’s efficiency in handling multi-dimensional data and performing element-wise calculations on matrices. I also practiced matrix multiplication using the %*% operator, reinforcing the distinction between component-wise operations and standard matrix algebra in R. These experiences have built my confidence in manipulating multi-dimensional data and have prepared me for more complex data analysis tasks involving matrix operations.
# 
# Styling the dataset table with kableExtra provided additional insights into the importance of data presentation. By creating a polished and visually engaging table, I was able to highlight key columns, add colors for readability, and implement hover effects for easier navigation. These formatting options made the data more accessible and professional, and I understood how presenting data clearly can improve interpretation and communication of results. It is not just the analysis that matters, but also how findings are displayed, as clear and well-organized tables make it easier for others (and myself) to interpret and use the results.
# 
# Overall, this project has significantly deepened my understanding of R’s data handling capabilities. Each operation, from loading datasets to creating variables and styling tables, contributed to my growing skill set. Interpreting these results has also shown me the importance of careful data manipulation, especially with R’s recycling rules and matrix handling. This experience has prepared me to apply these techniques in future projects, where I can produce efficient and visually appealing analyses that not only generate insights but also communicate them effectively.