Video summary
Lecture 15 : Introduction to R
Main summary
Key takeaways
Summary
1. What R and RStudio are
- R is a free, open-source statistical computing environment and programming language derived from the S language. It is used for statistical analysis, research, data science, graphics, and data manipulation.
- RStudio is an integrated development environment (IDE) for working with R. It provides tools for writing and running code and conducting data analysis.
- Both can be installed on commonly used operating systems, including Windows, macOS, Linux, and Unix. RStudio Cloud offers a browser-based way to work without installing R and RStudio locally, though its free use may have limits on capacity, speed, or storage.
- CRAN is a place to download R and find packages, manuals, news, and other R resources. The RStudio website is a source for downloading RStudio.
2. Getting oriented in RStudio
The lecture describes four main panes:
- Source editor: Write, edit, and save R scripts. Code typed here is not evaluated until it is sent to the Console. R scripts use the
.Rextension. - Console: R evaluates commands here and displays their results. Commands can be entered directly, though the lecture recommends writing scripts so code can be saved and reused.
- Environment/History pane: The Environment tab lists objects created in the current session, such as vectors and data frames. The History tab lists commands previously run.
- Files/Plots/Packages/Help pane: Use the Files tab to browse files and set the working directory, Plots to view and export plots, Packages to manage installed and loaded packages, and Help to look up functions.
Useful interface actions include:
- Run the current line or selected lines from a script with Ctrl+Enter or the Run button.
- Clear the Console with Ctrl+L or the broom icon.
- Find help through the Help pane or by entering
?followed by a function name.
3. Working directories and projects
A working directory is the folder R uses as the current location for reading and saving files. The lecture recommends setting one so project files are kept in a known place.
To set a working directory through RStudio:
- Open the Session menu.
- Choose Set Working Directory, then Choose Directory.
- Select the desired folder and confirm.
- Check that the Files pane shows the selected location. RStudio displays the corresponding
setwd()command, which can be copied into a script for reuse. - Use
getwd()to check the current working directory.
The default directory used when RStudio opens can be changed in Tools → Global Options.
An RStudio project organizes files for a particular activity—such as a course, thesis, or analysis—so its scripts, data, and intermediate outputs stay together.
To create or reopen a project:
- Choose File → New Project.
- Select a new or existing directory, then name or select it.
- Save the scripts and other relevant files in the project.
- Reopen it later through File → Open Project or the recent-projects list.
The lecture also demonstrates loading saved workspace data and advises checking what needs to be saved when closing a session.
4. Installing and loading packages
Packages are collections of R functions, data, and compiled code. R includes base packages, while additional packages can be downloaded and installed, commonly from CRAN.
To use an additional package:
- Install it through the Packages pane or with
install.packages(). - Load it into the current session with
library(package_name)or by checking it in the Packages pane. - Remember that installation and loading are separate: a package may be installed but not yet loaded in the current session.
The lecture demonstrates installing packages such as car, dplyr, lubridate, stringr, and zoo. Installation commands can be saved in a script and run again on another system.
5. Basic operators and functions
- Assignment: Assign values to variables, for example, setting
ato 3 andbto 5. - Arithmetic: Use
+,-,*,/, and^for addition, subtraction, multiplication, division, and exponentiation. - Functions: R includes functions for calculations and data work. Supply arguments in parentheses after a function name.
- Logarithms:
log()calculates logarithms. A base can be specified; if omitted, R uses the natural logarithm. Help and tab completion can be used to inspect a function’s syntax.
The lecture also introduces comparison and logical operations:
- Comparisons include
==,!=,>,<,>=, and<=, which produce logical results. &and|perform element-wise logical AND and OR operations.&&and||evaluate only the first element of each operand.- Logical values are
TRUEandFALSE; the lecture also shows the shorthandTandF.
6. Vectors
A vector is a fundamental R data structure. The c() function combines values into a vector.
Ways to create vectors:
- Combine specific values with
c(). - Create integer sequences with the colon operator, such as
5:13. - Use
seq()to control the start, end, and step size—or the number of values—in a sequence. - Use
head()andtail()to inspect the beginning or end of a vector.
Working with vectors:
- Arithmetic on vectors of the same length is performed element by element.
- If vectors have unequal lengths, R recycles the shorter vector’s values to complete the operation. R may issue a warning if the longer vector’s length is not a multiple of the shorter one.
sort()orders values; itsdecreasingoption reverses the order.- Square brackets select elements by position. One index selects one element, a range selects a span, and a set of indices selects particular positions. Negative indices exclude specified positions.
The lecture notes that vectors may contain numeric, character, or factor values.
7. Data types and summaries
The lecture introduces numeric, character, integer, factor, and logical types and demonstrates checking an object’s class with class().
- Numeric: Numbers, including decimal values.
- Character: Text and strings, generally written in quotes.
- Integer: Whole-number values.
- Logical:
TRUEorFALSE, often created by comparisons. - Factor: Categorical data, such as male/female categories, represented by levels.
The lecture also demonstrates:
- Converting values with functions such as
as.integer(),as.character(), andas.factor(). - Using
summary()to obtain a basic summary of an object. - Using
nchar()to count characters in character values. - Inspecting factor levels and category counts.
8. User-defined functions
Users can define their own functions to reuse calculations. The lecture illustrates creating a custom function to calculate a mean from a vector using R’s sum() and length() functions, then using that function in a custom standard-deviation example.
User-defined functions can package repeated calculations into reusable code, much like R’s built-in functions.
9. Conditional statements and loops
ifelse()applies a condition and returns one value where the condition is true and another where it is false. The lecture uses it to convert gender labels into shorter “M” and “F” outputs.- A
forloop repeats instructions over a sequence of indices. The example loops through the entries in a gender vector, checks each one, and prints the corresponding label.
The lecture presents these as introductory examples, with more advanced conditional statements and data-handling topics to come in later lessons.
Speakers and sources featured
- Speaker: An unnamed lecturer/narrator delivers the lesson; no individual speaker is identified in the subtitles.
- Resources and platforms mentioned: CRAN (the R Project package and download site), the R Journal and R manuals/help resources, RStudio’s website, and RStudio Cloud.
- Packages mentioned as examples:
car,dplyr,lubridate,stringr, andzoo. - Video source identified in the metadata: IIT Kanpur–NPTEL.
Rate this summary
Your feedback will help improve summaries.
Improve this summary
Reprocess with a stronger model when the summary feels incomplete or inaccurate.
Translate summary in another language
Ask questions to this video
Chat for follow-up questions, clarifications, and source-backed answers.