Beginners: R programming is used for statistical analysis and graphics which include a myriad of
data concepts. When dealing with statistics in R, there are different examples that can allow you to
benefit from data analysis.
Examples of Statistics in R:
Standard Deviation and Variance in R
Mean, Median, Mode
Probability
- Download R and R Studio
-
4 different windows in R:
- FIRST: Console: entering code
- Environment: Seeing Output
- Notes Section: Documentation
- Packages: Installed at R – API within
Data initialization:
- Add variables for x and y
- Can use multiplication and division operations * /
Creating a New File:
- Go to File, New File, R-Script (Cntrl Shift N)
Opening Existing File:
- File - Open File - Click on Existing File
To execute or run a code, highlight the line of code and click on Run or use CTRL + ENTER to run the
piece of code
- A new object X is created in the environment on the right
- To delete contents in the Console, use CTRL + L
- To delete contents in the Environment, use the function rm(). Place the variable in between
the parenthesis.
- rm(x) will delete the value of x in your Environment
Saving Script
- File - Save
Additional Features:
Pie Charts
- Use the pie() function to draw pie charts
- # Create a vector of pies
- x <- c(10,20,30,40)
- # Display the pie chart
The pie chart draws one pie for each value in the vector (ex: 10, 20, 30, 40)
default, the plotting of the first pie starts from the x-axis and moves counterclockwise
The value divided by the sum of all values: x/sum(x)
Labels and Headers
- The label parameter to add a label to the pie chart, and use the main parameter to a header.
#Create a vector of pies
- x <- c(10,20,30,40)
- #Create a vector of labels
- mylabel <- c("Apples", "Bananas", "Cherries", "Dates")
- #Display the pie chart with labels
- pie(x, label mylabel, main = "Fruits")
Legend
When adding a list of explanations for each pie, use the legend() function:
- #Create a vector of labels
- mylabel <- c("Apples", "Bananas", "Cherries", "Dates")
- #Create a vector of colors
- colors <- c("blue", "yellow", "green", "black")
# Display the pie chart with colors
pie(x, label mylabel, main = "Pie Chart", col colors)
# Display the explanation box
legend("bottomright", mylabel, fill colors)
Vertical Bar Graphs
To create a Bar Graph, a vector (H. c(Values...)) is taken that contains numeral values to be used
> # Create the data for the chart
A<- c(17, 32, 8, 53, 1)
#Plot the bar chart
barplot(A, xlab "X-axis", ylab "Y-axis", main "Bar-Chart")
To create a horizontal bar chart:
Take all parameter needed to make a simple bar chart
Then make it horizontal new parameter is added
Data Values: Bar Graphs:
cex.main, cex.lab, and cex.axis: Controls the font size of the chart title, x-axis label, and y-axis.
text(): The text() function adds data labels on top of each bar.
- The x argument specifies the x-coordinates of the labels,
- y argument adds a value of 1 to the corresponding bar heights to position the labels just
above the bars.
Downloading CSV Files:
Excel Sheet: File - Download as - CSV
Go to Downloads folder on Computer: Name it appropriately
Reading CSV: Open the CSV file and view your data and create graphs accordingly if needed and
copy and paste into a google doc for data extraction and article analysis