KEMBAR78
R Programming | PDF | Computer Programming | Variable (Computer Science)
0% found this document useful (0 votes)
16 views23 pages

R Programming

R is a widely used programming language for statistical computing and data visualization, offering various statistical techniques and graphical capabilities. It is open-source, platform-independent, and supported by a large community with numerous packages available for different tasks. The document covers R syntax, variable creation, data types, and basic operations, providing examples for better understanding.

Uploaded by

Danish Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views23 pages

R Programming

R is a widely used programming language for statistical computing and data visualization, offering various statistical techniques and graphical capabilities. It is open-source, platform-independent, and supported by a large community with numerous packages available for different tasks. The document covers R syntax, variable creation, data types, and basic operations, providing examples for better understanding.

Uploaded by

Danish Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

R Programming

• R is a popular programming language used for statistical


computing and graphical presentation.
• Its most common use is to analyze and visualize data.
• It is a GNU project which is similar to the S language and
environment which was developed at Bell Laboratories (formerly
AT&T, now Lucent Technologies) by John Chambers and
colleagues.
Why Use R?

• It is a great resource for data analysis, data visualization, data science and
machine learning.
• It provides many statistical techniques (such as statistical tests,
classification, clustering and data reduction).
• It is easy to draw graphs in R, like pie charts, histograms, box plot, scatter
plot, etc.
• It works on different platforms (Windows, Mac, Linux).
• It is open-source and free.
• It has a large community support.
• It has many packages (libraries of functions) that can be used to solve
different problems.
Examples Visualization
Examples Visualization
R Syntax
• To output text in R, use single or doubleweotes:
• Example:
"Hello World!"

• To output numbers, just type the number (without quotes):


• Example:
5
10
25

• To do simple calculations, add numbers together:


• Example:
5+5
R Print Output
Unlike many other programming languages, we can output code in R without using a print
function:
Example:
“Hello World!”
However, R does have a print() function available if we want to use it. This might be useful if we
are familiar with other programming languages, such as Python, which often uses the print()
function to output code.
Example
print("Hello World!")
And there are times we must use the print() function to output code, for example when working
with for loops.
Example:
for (x in 1:10) {
print(x)
}
R Comments
• Comments can be used to explain R code, and to make it more readable. It can also be used to prevent execution when
testing alternative code.
• Comments starts with a #. When executing code, R will ignore anything that starts with #.
• This example uses a comment before a line of code:
Example
# This is a comment
"Hello World!“
• This example uses a comment at the end of a line of code:
Example
"Hello World!" # This is a comment
• Unlike other programming languages, such as Java, there are no syntax in R for multiline comments. However, we can just
insert a # for each line to create multiline comments:
Example:
# This is a comment
# written in
# more than just one line
"Hello World!"
R Variables

• Creating Variables in R:-


• Variables are containers for storing data values.
• R does not have a command for declaring a variable.
• A variable is created the moment we first assign a value to it.
• To assign a value to a variable, use the <- sign.
• To output (or print) the variable value, just type the variable name:
Example:-
name <- "John"
age <- 40
name # output "John"
age # output 40
Print / Output Variables
• Compared to many other programming languages, we do not have to use a function to print/output
variables in R. we can just type the name of the variable:
Example:
name <- "John Doe"
name # auto-print the value of the name variable
• However, R does have a print() function available if we want to use it. This might be useful if we are
familiar with other programming languages, such as Python, which often use a print() function to output
variables.
Example:-
name <- "John Doe"
print(name) # print the value of the name variable
R Concatenate Elements
• We can concatenate, or join, two or more elements, by using the paste() function.
• To combine both text and a variable, R uses comma (,):
• Example:
text <- "awesome"
paste("R is", text)
Output: ”R is awesome”.
• we can also use , to add a variable to another variable:
• Example:
text1 <- "R is"
text2 <- "awesome"
paste(text1, text2)
Output:- ”R is awesome”.
• For numbewe the + character works as a mathematical operator:
• Example:
num1 <- 5
num2 <- 10
num1 + num2
Output:- 15
• If we try to combine a string (text) and a number, R will give an error:
• Example:-
num <- 5
text <- "Some text"
num + text
Output:- Error in num + text : non-numeric argument to binary operator.
R Multiple Variables
• R allows us to assign the same value to multiple variables in one line:
• Example:
# Assign the same value to multiple variables in one line
var1 <- var2 <- var3 <- "Orange"
# Print variable values
var1
var2
var3
Output:- “Orange”
“Orange”
“Orange”.
R Variable Names (Identifiers)
A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume).
Rules for R variables are:
• A variable name must start with a letter and can be a combination of
letters, digits, period(.) and underscore(_). If it starts with period(.), it
cannot be followed by a digit.
• A variable name cannot start with a number or underscore (_)
• Variable names are case-sensitive (age, Age and AGE are three
different variables)
• Reserved words cannot be used as variables (TRUE, FALSE, NULL, if...)
• # Legal variable names:
myvar <- "John"
my_var <- "John"
myVar <- "John"
MYVAR <- "John"
myvar2 <- "John"
.myvar <- "John"
# Illegal variable names:
2myvar <- "John"
my-var <- "John"
my var <- "John"
_my_var <- "John"
my_v@ar <- "John"
TRUE <- "John“
• Note:- Remember that variable names are case-sensitive!
R Data Types
• In programming, data type is an important concept.
• Variables can store data of different types, and different types can
do different things.
• In R, variables do not need to be declared with any particular
type, and can even change type after they have been set:
• Example:
my_var <- 30 # my_var is type of numeric
my_var <- "Sally" # my_var is now of type character (aka string)
• R has a variety of data types and object classes. we will learn
much more about these as we continue to get to know R.
Basic Data Types
Basic data types in R can be divided into the following types:
• numeric - (10.5, 55, 787)
• integer - (1L, 55L, 100L, where the letter "L" declares this as an integer)
• complex - (9 + 3i, where "i" is the imaginary part)
• character (a.k.a. string) - ("k", "R is exciting", "11.5")
• logical (a.k.a. boolean) - (TRUE or FALSE)
Note:- We can use the class() function to check the data type of a
variable:
Example:-
# numeric
x <- 10.5
class(x)

# integer
x <- 1000L
class(x)

# complex
x <- 9i + 3
class(x)

# character/string
x <- "R is exciting"
class(x)

# logical/boolean
x <- TRUE
class(x)
R Numbers
There are three number types in R:
1. Numeric
2. Integer
3. Complex
• Variables of number types are created when we assign a value to
them:
• Example:
x <- 10.5 # numeric
y <- 10L # integer
z <- 1i # complex
• Numeric:-
A numeric data type is the most common type in R, and contains any number with or without a
decimal, like: 10.5, 55, 787:
Example:
x <- 10.5
y <- 55
# Print values of x and y
x
y
# Print the class name of x and y
class(x)
class(y)
• Integer:-
Integers are numeric data without decimals. This is used when we are certain that we will
never create a variable that should contain decimals. To create an integer variable, we must
use the letter L after the integer value:
Example:
x <- 1000L
y <- 55L

# Print values of x and y


x
y

# Print the class name of x and y


class(x)
class(y)
• Complex:-
A complex number is written with an "i" as the imaginary part:
Example:-
x <- 3+5i
y <- 5i
# Print values of x and y
x
y
# Print the class name of x and y
class(x)
class(y)
Type Conversion
we can convert from one type to another with the following functions:
• as.numeric()
• as.integer()
• as.complex()
• Example:
• x <- 1L # integer
y <- 2 # numeric
# convert from integer to numeric:
a <- as.numeric(x)
# convert from numeric to integer:
b <- as.integer(y)
# print values of x and y
x
y
# print the class name of a and b
class(a)
class(b)

You might also like