KEMBAR78
4 R and RStudio 2 | PDF | Variable (Computer Science) | Data Type
0% found this document useful (0 votes)
22 views20 pages

4 R and RStudio 2

R studio thapar

Uploaded by

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

4 R and RStudio 2

R studio thapar

Uploaded by

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

Introduction to R and Rstudio

(Part 2)
Contents

Basic syntax, variables, Operators, Data types


Basic syntax
Results of calculations can be stored in objects using the assignment operators:
◦ An arrow (<-) formed by a smaller than character and a hyphen without a space!
◦ The equal character (=).
Sample Example
◦ > n <- 15
◦ >n
[1] 15
◦ > a = 12
◦ >a

 To see a list of your objects, use ls( ). The ( ) is required, even though there are no
arguments.
 Use rm to delete objects you no longer need.
> rm(n) #here n is object name

 fdndf
Basic syntax
Creating Strings- use either double quote characters or single
quotes
> h <- "hello“ or > h <- 'hello'
Other data types are available. You do not need to declare these;
they will be assigned automatically.
> q1 <- TRUE # Logical data
> q1
[1] TRUE
> q2 <- F
> q2
[1] FALSE
Variables
Variables are used to store the information to be manipulated and referenced in
the R program.
Variables must start with a letter, but may also contain numbers and periods.
The preferred form for variable names is all lower case letters and words
separated with dots (variable.name) but variableName is also accepted.
Don't use underscores ( _ ) or hyphens ( - ) in identifiers.
Examples:
avg.clicks GOOD
avgClicks OK
avg_Clicks BAD

Function names have initial capital letters and no dots (e.g., FunctionName).
Data types
Data types
Practice Examples
#Logical Data type
variable_logical<- TRUE
cat(variable_logical,"\n")
cat("The data type of variable_logical is ",class(variable_logical),"
\n\n")

#Numeric Data type


variable_numeric<- 3532
cat(variable_numeric,"\n")
cat("The data type of variable_numeric is ",class(variable_numeri
c),"\n\n")

#Integer Data type


variable_integer<- 133L
cat(variable_integer,"\n")
cat("The data type of variable_integer is ",class(variable_integer)
,"\n\n")
Practice Examples
#Complex Data type
variable_complex<- 3+2i
cat(variable_complex,"\n")
cat("The data type of variable_complex is ",class(variable_compl
ex),"\n\n")

#Character Data type


variable_char<- "Learning r programming"
cat(variable_char,"\n")
cat("The data type of variable_char is ",class(variable_char),"\n\
n")

#Raw Data type


variable_raw<- charToRaw("Learning r programming")
cat(variable_raw,"\n")
cat("The data type of variable_char is ",class(variable_raw),"\n\
n")
Operators
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Miscellaneous Operators
Operators
R may be used for simple calculation, using the standard arithmetic
symbols +, -, *, /, as well as parentheses and ^ (exponentiation).
> a <- 12+14
>a
[1] 26
> 3*5
[1] 15
> (20-4)/2
[1] 8
> 7^2
[1] 49
Standard mathematical functions
Standard mathematical functions are available.
> exp(2)
[1] 7.389056
> log(10)# Natural log
[1] 2.302585
> log10(10) # Base 10
[1] 1
> log2(64) # Base 2
[1] 6
Standard mathematical functions
> pi
[1] 3.141593
> cos(pi)
[1] -1
> sqrt(100)
[1] 10
Operators
A relational operator is a symbol which defines some kind of relation
between two entities. These include numerical equalities and inequalities. A
relational operator compares each element

> x=5 > print(x<=y)


> y=45 [1] TRUE
> print(x>y) > print(x==y)
[1] FALSE [1] FALSE
> print(x<y) > print(x!=y)
[1] TRUE [1] TRUE
> print(x>=y)
[1] FALSE
Operators
The logical operators allow a program to make a decision on the basis of
multiple conditions.

> x=5
> y=45
> print(x&y)
[1] TRUE
print(x|y)
[1] TRUE
> print(!y)
[1] FALSE
Operators
An assignment operator is used to assign a new value to a variable.

> a <- 25
>a
[1] 25
> a = 12
>a
[1] 12
> 24 -> x
>x
[1] 24
Operators
Miscellaneous operators are used for a special and specific purpose.

> a <- 1:5


>a
[1] 1 2 3 4 5
>x1 <- 5
>x2 <- 10
>d <- 1:7
>print(x1%in%d)
[1] TRUE
>print(x2%in%d)
[1]FALSE
R commands in integrated environment
Practice Examples
> 3+5
> 12 + 3 / 4 – 5 + 3*8
> (12 + 3 / 4 – 5) + 3*8
> pi * 2^3 – sqrt(4)
>factorial(4) Note
>log(2,10)  R ignores spaces

>log(2, base=10)
>log10(2)
>log(2)
Practice Examples
> x = 3+5
>x
> y = 12 + 3 / 4 – 5 + 3*8
>y
> z = (12 + 3 / 4 – 5) + 3*8
>z
> A <- 6 + 8 ## no space should be between < & -
>a ## Note: R is case sensitive
>A

You might also like