Download R-4.3.2 for Windows. The R-project for statistical computing.https://cran.r-project.org/bin/windows/base/
RStudio Desktop - Posithttps://posit.co/download/rstudio-desktop/
Q&A: why we need to download both R and Rstudio when learning
A:
R Programming Language:
RStudio IDE:
Enhanced User Experience:
IDE Features:
Project Management:
Graphical User Interface (GUI):
How to import a data frame and its head (for a pre-install data frame)
# Import the pre-installed dataset (mtcars)
data(mtcars)
# Display the head of the dataset
head(mtcars)
# When you call head(mtcars), it shows the first six rows of the mtcars dataset.
If you want to know what datasets are available, you can use the datasets
package, which is typically pre-installed with R. The data()
function automatically loads datasets from the datasets
package.
# Load the datasets package
library(datasets)
# List available datasets
data()
If you want to display all the information for a given object in R, you can use the str()
function. The str()
function provides a compact display of the internal structure of an R object. It is particularly useful for examining the structure of complex objects like data frames, lists, or models. Here's how you can use it:
# Assuming 'your_object' is the object you want to examine
str(your_object)
This will print a concise summary of the object's structure, showing you the data type and the first few elements of the object. It's a quick way to get an overview of the content and structure of your object.
Additionally, if you want to see the entire contents of a data frame or matrix in the console, you can simply type the name of the object and press Enter:
# Assuming 'your_data_frame' is a data frame
your_data_frame
This will display the entire data frame in the console. Note that this approach is practical for small to moderately-sized datasets, but for larger datasets, it might not be practical to display the entire content in the console. In such cases, using functions like head()
or summary()
may be more suitable for a concise overview.
Use indexing to view specific columns of your dataset.
# View the first 5 rows of the first two columns
your_data[1:5, 1:2]
Using Data Viewer in RStudio:
If you are using RStudio, you can use the built-in data viewer. Just type the name of your dataset in the console and press Enter, or use the View()
function:
View(your_data)
In R, you can summarize one or more variables using various functions. Here are some key functions and methods for summarizing variables in R:
Summary Statistics:
summary()
: Provides a summary of the central tendency, dispersion, and distribution of a variable.
summary(your_variable)
Descriptive Statistics:
mean()
: Calculates the mean (average) of a variable.
mean(your_variable)
sd()
: Computes the standard deviation of a variable.
min()
and max()
: Return the minimum and maximum values of a variable.
Frequency Table:
table()
: Creates a frequency table for categorical variablesHistogram:
hist()
: Plots a histogram for visualizing the distribution of a numerical variable.Boxplot:
boxplot()
: Creates a boxplot to display the summary of a variable's distribution.Quantiles:
quantile()
: Calculates specific quantiles (e.g., median, quartiles) for a variable.Summary by Group:
tapply()
: Applies a function to subsets of a variable based on a grouping factor.
aggregate()
: Aggregates data by applying a function for each group.
You can try the
demo("graphics")
to get the demo code it provides you to work with graphics
You can see, to get a attractive and informative graph, it needs several lines of codes
Editing figures with R studio is not easy, but R studio has other advantages:
1.The Zoom function enables to better visualize the graph
2. Export function (or click right + save as) enables you to store easily high-quality figures (while Journals and reports require 200 dpi for line art, 600 api for grayscale and 300 dpi for color, at the correct size)
What is the working directory:
The working directory is the directory where your R dataset, report, script and so, will be stored. It might not be critical for this class, but it will be the day you will do run long term project. How to know where is your working directory?
>getwd()
You may find it on C/
Using the setwd() function
Purpose: Serves the temporary role of changing the working directory, only effective for the current RStudio window. Once the window is closed, it will revert to the default path. The getwd() function is used to retrieve the current working directory.
Setting global options (as shown in the figure)
First, in the tools menu, click on the global options option.
Then, in the default working directory section, you can modify the default working path by clicking browse to change the working directory.
Finally, save and reopen RStudio; the working directory will now be the modified path.
What is the R console, where it is
What is the R environment where it is
How to import a data frame and its head (for a pre-install data frame)
How to View your data
How to display basic information about your data (summary, sd)
Lunch an existing graph demo (demo("graphics"), and navigate from one graph to another
Find and set-up your working directory : getwd()