[R] First few steps for using R and Rstudio

2024-01-10 13:52:54

R:

Download R-4.3.2 for Windows. The R-project for statistical computing.icon-default.png?t=N7T8https://cran.r-project.org/bin/windows/base/

Rstudio:

RStudio Desktop - Positicon-default.png?t=N7T8https://posit.co/download/rstudio-desktop/

Q&A: why we need to download both R and Rstudio when learning

A:?

  1. R Programming Language:

    • Core statistical computing and graphics.
    • Command-line interface.
  2. RStudio IDE:

    • Integrated Development Environment.
    • User-friendly interface.
    • Script editor, console, data viewer, and plot viewer.
  3. Enhanced User Experience:

    • Simplifies code writing, debugging, and running.
    • Interactive and visually appealing.
  4. IDE Features:

    • Syntax highlighting, code completion.
    • Version control integration.
  5. Project Management:

    • Built-in project management.
    • Organizes workspace, scripts, and data.
  6. Graphical User Interface (GUI):

    • Easier visualization of plots and charts.
    • More intuitive for beginners.

Import

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()

what if I want to display all the info for a given object?

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)

????????

Summarize Function

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 variables

Histogram:

  • 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.

demo of graphics

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)

Setup Working directory:

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.
?

Summary:

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()

文章来源:https://blog.csdn.net/m0_74331272/article/details/135495731
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。