Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
58 views6 pages

Introduction To R: Vector Arithmetic

This document introduces vector arithmetic in R. It shows that computations are performed element-wise on vectors, so that mathematical operations like addition, subtraction, multiplication and division output a result for each element in the vectors. It also demonstrates using functions like sum() to calculate totals over elements in a vector, and logical operators like > to compare vector elements.

Uploaded by

seggy7
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)
58 views6 pages

Introduction To R: Vector Arithmetic

This document introduces vector arithmetic in R. It shows that computations are performed element-wise on vectors, so that mathematical operations like addition, subtraction, multiplication and division output a result for each element in the vectors. It also demonstrates using functions like sum() to calculate totals over elements in a vector, and logical operators like > to compare vector elements.

Uploaded by

seggy7
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/ 6

INTRODUCTION TO R

Vector Arithmetic

Introduction to R

Vector Arithmetic
> my_apples <- 5
> my_oranges <- 6
> my_apples + my_oranges
[1] 11

my_apples is a vector!
my_oranges is a vector!

Computations are performed element-wise


> earnings <- c(50, 100, 30)
> earnings * 3
[1] 150 300 90

Introduction to R

Vector Arithmetic
> earnings/10
[1] 5 10 3
> earnings - 20
[1] 30 80 10
> earnings + 100
[1] 150 200 130
> earnings^2
[1] 2500 10000

900

Mathematics naturally extend!

Introduction to R

Element-wise
> earnings <- c(50, 100, 30)
> expenses <- c(30, 40, 80)
> earnings - expenses
[1] 20 60 -50
> earnings + c(10, 20, 30)
[1] 60 120 60
> earnings * c(1, 2, 3)
[1] 50 200 90
> earnings / c(1, 2, 3)
[1] 50 50 10

multiplication and division


are done element-wise!

Introduction to R

sum() and >


> earnings <- c(50, 100, 30)
> expenses <- c(30, 40, 80)
> bank <- earnings - expenses
> bank
[1] 20 60 -50
> sum(bank)
[1] 30
> earnings > expenses
[1] TRUE TRUE FALSE

INTRODUCTION TO R

Lets practice!

You might also like